LLVM 23.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"
13
14using namespace llvm;
15using namespace llvm::cas;
16using namespace llvm::cas::builtin;
17
18static StringRef getCASIDPrefix() { return "llvmcas://"; }
19void BuiltinCASContext::anchor() {}
20
22 if (!Reference.consume_front(getCASIDPrefix()))
23 return createStringError(std::make_error_code(std::errc::invalid_argument),
24 "invalid cas-id '" + Reference + "'");
25
26 // FIXME: Allow shortened references?
27 if (Reference.size() != 2 * sizeof(HashType))
28 return createStringError(std::make_error_code(std::errc::invalid_argument),
29 "wrong size for cas-id hash '" + Reference + "'");
30
31 std::string Binary;
32 if (!tryGetFromHex(Reference, Binary))
33 return createStringError(std::make_error_code(std::errc::invalid_argument),
34 "invalid hash in cas-id '" + Reference + "'");
35
36 assert(Binary.size() == sizeof(HashType));
37 HashType Digest;
38 llvm::copy(Binary, Digest.data());
39 return Digest;
40}
41
44 if (!Digest)
45 return Digest.takeError();
46
47 return CASID::create(&getContext(), toStringRef(*Digest));
48}
49
51 SmallString<64> Hash;
52 toHex(Digest, /*LowerCase=*/true, Hash);
53 OS << getCASIDPrefix() << Hash;
54}
55
56void BuiltinCASContext::printIDImpl(raw_ostream &OS, const CASID &ID) const {
57 BuiltinCASContext::printID(ID.getHash(), OS);
58}
59
61 static BuiltinCASContext DefaultContext;
62 return DefaultContext;
63}
64
70
72 auto Ref = getReference(ID);
73 if (!Ref)
75
76 auto Handle = load(*Ref);
77 if (!Handle)
78 return Handle.takeError();
79
80 auto Proxy = ObjectProxy::load(*this, *Ref, *Handle);
82 if (auto E = Proxy.forEachReference([&](ObjectRef Ref) -> Error {
83 Refs.push_back(Ref);
84 return Error::success();
85 }))
86 return E;
87
88 ArrayRef<char> Data(Proxy.getData().data(), Proxy.getData().size());
89 auto Hash = BuiltinObjectHasher<HasherT>::hashObject(*this, Refs, Data);
90 if (!ID.getHash().equals(Hash))
92
93 return Error::success();
94}
95
98#if LLVM_ENABLE_ONDISK_CAS
99 return ondisk::UnifiedOnDiskCache::open(Path, /*SizeLimit=*/std::nullopt,
101 sizeof(HashType));
102#else
103 return createStringError(inconvertibleErrorCode(), "OnDiskCache is disabled");
104#endif
105}
106
109 SmallVectorImpl<uint8_t> &Result) {
110 auto Hash =
112 Result.assign(Hash.begin(), Hash.end());
113}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Mark last scratch load
static StringRef getCASIDPrefix()
This file contains some functions that are useful when dealing with strings.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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 class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:117
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 LLVM_ABI 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
void hashingFunc(ArrayRef< ArrayRef< uint8_t > > Refs, ArrayRef< char > Data, SmallVectorImpl< uint8_t > &Result)
Convenience wrapper for BuiltinObjectHasher.
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:94
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:1321
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
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.