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"
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 {
58}
59
61 static BuiltinCASContext DefaultContext;
62 return DefaultContext;
63}
64
68 Refs, Data);
69}
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}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Mark last scratch load
static StringRef getCASIDPrefix()
Definition: BuiltinCAS.cpp:18
Provides a library for accessing information about this process and other processes on the operating ...
raw_pwrite_stream & OS
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.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Hasher for stored objects in builtin CAS.
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)
Definition: ObjectStore.h:280
Reference to an object in an ObjectStore instance.
Definition: CASReference.h:108
const CASContext & getContext() const
Get CASContext.
Definition: ObjectStore.h:222
virtual std::optional< ObjectRef > getReference(const CASID &ID) const =0
Get an existing reference to the object called ID.
CASContext for LLVM builtin CAS using BLAKE3 hash type.
static const BuiltinCASContext & getDefaultContext()
Definition: BuiltinCAS.cpp:60
static Expected< HashType > parseID(StringRef PrintedDigest)
Definition: BuiltinCAS.cpp:21
static void printID(ArrayRef< uint8_t > Digest, raw_ostream &OS)
Definition: BuiltinCAS.cpp:50
Error createUnknownObjectError(const CASID &ID) const
Definition: BuiltinCAS.h:53
Error validate(const CASID &ID) final
Validate the underlying object referred by CASID.
Definition: BuiltinCAS.cpp:71
Expected< CASID > parseID(StringRef Reference) final
Get a CASID from a ID, which should have been generated by CASID::print().
Definition: BuiltinCAS.cpp:42
Error createCorruptObjectError(const CASID &ID) const
Definition: BuiltinCAS.h:58
Expected< ObjectRef > store(ArrayRef< ObjectRef > Refs, ArrayRef< char > Data) final
Store object into ObjectStore.
Definition: BuiltinCAS.cpp:65
virtual Expected< ObjectRef > storeImpl(ArrayRef< uint8_t > ComputedHash, ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)=0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
decltype(HasherT::hash(std::declval< ArrayRef< uint8_t > & >())) HashType
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1854