LLVM 22.0.0git
BuiltinCAS.cpp
Go to the documentation of this file.
1//===- BuiltinCAS.cpp -------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "BuiltinCAS.h"
14
15using namespace llvm;
16using namespace llvm::cas;
17using namespace llvm::cas::builtin;
18
19static StringRef getCASIDPrefix() { return "llvmcas://"; }
20void BuiltinCASContext::anchor() {}
21
23 if (!Reference.consume_front(getCASIDPrefix()))
24 return createStringError(std::make_error_code(std::errc::invalid_argument),
25 "invalid cas-id '" + Reference + "'");
26
27 // FIXME: Allow shortened references?
28 if (Reference.size() != 2 * sizeof(HashType))
29 return createStringError(std::make_error_code(std::errc::invalid_argument),
30 "wrong size for cas-id hash '" + Reference + "'");
31
32 std::string Binary;
33 if (!tryGetFromHex(Reference, Binary))
34 return createStringError(std::make_error_code(std::errc::invalid_argument),
35 "invalid hash in cas-id '" + Reference + "'");
36
37 assert(Binary.size() == sizeof(HashType));
38 HashType Digest;
39 llvm::copy(Binary, Digest.data());
40 return Digest;
41}
42
45 if (!Digest)
46 return Digest.takeError();
47
48 return CASID::create(&getContext(), toStringRef(*Digest));
49}
50
52 SmallString<64> Hash;
53 toHex(Digest, /*LowerCase=*/true, Hash);
54 OS << getCASIDPrefix() << Hash;
55}
56
57void BuiltinCASContext::printIDImpl(raw_ostream &OS, const CASID &ID) const {
58 BuiltinCASContext::printID(ID.getHash(), OS);
59}
60
62 static BuiltinCASContext DefaultContext;
63 return DefaultContext;
64}
65
71
73 auto Ref = getReference(ID);
74 if (!Ref)
76
77 auto Handle = load(*Ref);
78 if (!Handle)
79 return Handle.takeError();
80
81 auto Proxy = ObjectProxy::load(*this, *Ref, *Handle);
83 if (auto E = Proxy.forEachReference([&](ObjectRef Ref) -> Error {
84 Refs.push_back(Ref);
85 return Error::success();
86 }))
87 return E;
88
89 ArrayRef<char> Data(Proxy.getData().data(), Proxy.getData().size());
90 auto Hash = BuiltinObjectHasher<HasherT>::hashObject(*this, Refs, Data);
91 if (!ID.getHash().equals(Hash))
93
94 return Error::success();
95}
96
99#if LLVM_ENABLE_ONDISK_CAS
100 return ondisk::UnifiedOnDiskCache::open(Path, /*SizeLimit=*/std::nullopt,
102 sizeof(HashType));
103#else
104 return createStringError(inconvertibleErrorCode(), "OnDiskCache is disabled");
105#endif
106}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Mark last scratch load
static StringRef getCASIDPrefix()
Provides a library for accessing information about this process and other processes on the operating ...
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static HashT hashObject(const ObjectStore &CAS, ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)
Unique identifier for a CAS object.
Definition CASID.h:58
static CASID create(const CASContext *Context, StringRef Hash)
Create CASID from CASContext and raw hash bytes.
Definition CASID.h:116
static ObjectProxy load(ObjectStore &CAS, ObjectRef Ref, ObjectHandle Node)
Reference to an object in an ObjectStore instance.
const CASContext & getContext() const
Get CASContext.
virtual std::optional< ObjectRef > getReference(const CASID &ID) const =0
Get an existing reference to the object called ID.
static const BuiltinCASContext & getDefaultContext()
static Expected< HashType > parseID(StringRef PrintedDigest)
static void printID(ArrayRef< uint8_t > Digest, raw_ostream &OS)
static StringRef getHashName()
Get the name of the hash for any table identifiers.
Error createUnknownObjectError(const CASID &ID) const
Definition BuiltinCAS.h:56
Error validateObject(const CASID &ID) final
Validate the underlying object referred by CASID.
Expected< CASID > parseID(StringRef Reference) final
Get a CASID from a ID, which should have been generated by CASID::print().
Error createCorruptObjectError(const CASID &ID) const
Definition BuiltinCAS.h:61
Expected< ObjectRef > store(ArrayRef< ObjectRef > Refs, ArrayRef< char > Data) final
Store object into ObjectStore.
virtual Expected< ObjectRef > storeImpl(ArrayRef< uint8_t > ComputedHash, ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)=0
static Expected< std::unique_ptr< UnifiedOnDiskCache > > open(StringRef Path, std::optional< uint64_t > SizeLimit, StringRef HashName, unsigned HashByteSize, OnDiskGraphDB::FaultInPolicy FaultInPolicy=OnDiskGraphDB::FaultInPolicy::FullTree)
Open a UnifiedOnDiskCache instance for a directory.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
decltype(HasherT::hash(std::declval< ArrayRef< uint8_t > & >())) HashType
Expected< std::unique_ptr< ondisk::UnifiedOnDiskCache > > createBuiltinUnifiedOnDiskCache(StringRef Path)
Create a UnifiedOnDiskCache instance that uses BLAKE3 hashing.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
bool tryGetFromHex(StringRef Input, std::string &Output)
Convert hexadecimal string Input to its binary representation and store the result in Output....
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1835
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
StringRef toStringRef(bool B)
Construct a string ref from a boolean.