Skip to content

Commit 55957ad

Browse files
NikitaRudenkoIntelvladimirlaz
authored andcommitted
Add translation of memory model from module metadata
1 parent b90645a commit 55957ad

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,6 +3345,11 @@ bool SPIRVToLLVM::transMetadata() {
33453345
getMDNodeStringIntVec(Context, EM->getLiterals()));
33463346
}
33473347
}
3348+
NamedMDNode *MemoryModelMD =
3349+
M->getOrInsertNamedMetadata(kSPIRVMD::MemoryModel);
3350+
MemoryModelMD->addOperand(
3351+
getMDTwoInt(Context, static_cast<unsigned>(BM->getAddressingModel()),
3352+
static_cast<unsigned>(BM->getMemoryModel())));
33483353
return true;
33493354
}
33503355

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ static void foreachKernelArgMD(
105105
}
106106
}
107107

108+
static SPIRVMemoryModelKind getMemoryModel(Module &M) {
109+
auto *MemoryModelMD = M.getNamedMetadata(kSPIRVMD::MemoryModel);
110+
if (MemoryModelMD && (MemoryModelMD->getNumOperands() > 0)) {
111+
auto *Ref0 = MemoryModelMD->getOperand(0);
112+
if (Ref0 && Ref0->getNumOperands() > 1) {
113+
auto &&ModelOp = Ref0->getOperand(1);
114+
auto *ModelCI = mdconst::dyn_extract<ConstantInt>(ModelOp);
115+
if (ModelCI && (ModelCI->getValue().getActiveBits() <= 64)) {
116+
auto Model = static_cast<SPIRVMemoryModelKind>(ModelCI->getZExtValue());
117+
return Model;
118+
}
119+
}
120+
}
121+
return SPIRVMemoryModelKind::MemoryModelMax;
122+
}
123+
108124
LLVMToSPIRV::LLVMToSPIRV(SPIRVModule *SMod)
109125
: ModulePass(ID), M(nullptr), Ctx(nullptr), BM(SMod), SrcLang(0),
110126
SrcLangVer(0) {
@@ -2362,7 +2378,7 @@ bool LLVMToSPIRV::translate() {
23622378
for (auto I : Defs)
23632379
transFunction(I);
23642380

2365-
if (!transOCLKernelMetadata())
2381+
if (!transMetadata())
23662382
return false;
23672383
if (!transExecutionMode())
23682384
return false;
@@ -2565,7 +2581,18 @@ void LLVMToSPIRV::transFPContract() {
25652581
}
25662582
}
25672583

2568-
bool LLVMToSPIRV::transOCLKernelMetadata() {
2584+
bool LLVMToSPIRV::transMetadata() {
2585+
if (!transOCLMetadata())
2586+
return false;
2587+
2588+
auto Model = getMemoryModel(*M);
2589+
if (Model != SPIRVMemoryModelKind::MemoryModelMax)
2590+
BM->setMemoryModel(static_cast<SPIRVMemoryModelKind>(Model));
2591+
2592+
return true;
2593+
}
2594+
2595+
bool LLVMToSPIRV::transOCLMetadata() {
25692596
for (auto &F : *M) {
25702597
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
25712598
continue;

llvm-spirv/lib/SPIRV/SPIRVWriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class LLVMToSPIRV : public ModulePass {
173173
SPIRVWord *EntryPoint = nullptr,
174174
SmallVectorImpl<std::string> *Dec = nullptr);
175175
bool oclIsKernel(Function *F);
176-
bool transOCLKernelMetadata();
176+
bool transMetadata();
177+
bool transOCLMetadata();
177178
SPIRVInstruction *transBuiltinToInst(StringRef DemangledName, CallInst *CI,
178179
SPIRVBasicBlock *BB);
179180
SPIRVValue *transBuiltinToConstant(StringRef DemangledName, CallInst *CI);

llvm-spirv/test/memory_model_md.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -o %t.spv
3+
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
4+
; RUN: llvm-spirv -r %t.spv -o %t.bc
5+
; RUN: llvm-dis %t.bc -o %t.ll
6+
; RUN: FileCheck %s --input-file %t.ll -check-prefix=LLVM
7+
; RUN: FileCheck %s --input-file %t.spt -check-prefix=SPV
8+
9+
; ModuleID = 'float_control_empty.bc'
10+
source_filename = "float_control_empty.cpp"
11+
target datalayout = "e-p:64:64-i64:64-n8:16:32"
12+
target triple = "spir"
13+
14+
; LLVM: !spirv.MemoryModel = !{![[MMMD:[0-9]+]]}
15+
; LLVM: ![[MMMD]] = !{i32 {{[0-9]+}}, i32 0}
16+
; SPV: MemoryModel 1 0
17+
; Function Attrs: noinline norecurse nounwind readnone
18+
define dso_local dllexport void @k_no_fc(i32 %ibuf, i32 %obuf) local_unnamed_addr #16 {
19+
entry:
20+
ret void
21+
}
22+
23+
attributes #16 = { noinline norecurse nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
24+
25+
!llvm.module.flags = !{!1}
26+
!llvm.ident = !{!2}
27+
!spirv.MemoryModel = !{!0}
28+
29+
!0 = !{i32 1, i32 0}
30+
!1 = !{i32 1, !"wchar_size", i32 4}
31+
!2 = !{!"clang version 8.0.1"}

0 commit comments

Comments
 (0)