Skip to content

Commit e7f6c8c

Browse files
committed
[NFC] Add a ConstantInitBuilder method to add a UniqueHash
structure for a string
1 parent 24616ae commit e7f6c8c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/IRGen/ConstantBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class ConstantAggregateBuilderBase
156156
void addSignedPointer(llvm::Constant *pointer,
157157
const clang::PointerAuthSchema &schema,
158158
uint16_t otherDiscriminator);
159+
160+
/// Add a UniqueHash metadata structure to this builder which stores
161+
/// a hash of the given string.
162+
void addUniqueHash(StringRef ofString);
159163
};
160164

161165
class ConstantArrayBuilder

lib/IRGen/GenConstant.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include "TypeInfo.h"
2424
#include "StructLayout.h"
2525
#include "Callee.h"
26+
#include "ConstantBuilder.h"
2627
#include "swift/Basic/Range.h"
2728
#include "swift/SIL/SILModule.h"
29+
#include "llvm/Support/BLAKE3.h"
2830

2931
using namespace swift;
3032
using namespace irgen;
@@ -282,3 +284,11 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
282284
insertPadding(elts, sTy);
283285
return llvm::ConstantStruct::get(sTy, elts);
284286
}
287+
288+
void ConstantAggregateBuilderBase::addUniqueHash(StringRef data) {
289+
llvm::BLAKE3 hasher;
290+
hasher.update(data);
291+
auto rawHash = hasher.final();
292+
auto truncHash = llvm::makeArrayRef(rawHash).slice(0, NumBytes_UniqueHash);
293+
add(llvm::ConstantDataArray::get(IGM().getLLVMContext(), truncHash));
294+
}

0 commit comments

Comments
 (0)