Skip to content

Commit 0d1a32e

Browse files
NikitaRudenkoIntelAlexeySotkin
authored andcommitted
Add translation of memory model from module metadata
1 parent bc97fa6 commit 0d1a32e

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

lib/SPIRV/SPIRVReader.cpp

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

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) {
@@ -2354,7 +2370,7 @@ bool LLVMToSPIRV::translate() {
23542370
for (auto I : Defs)
23552371
transFunction(I);
23562372

2357-
if (!transOCLKernelMetadata())
2373+
if (!transMetadata())
23582374
return false;
23592375
if (!transExecutionMode())
23602376
return false;
@@ -2557,7 +2573,18 @@ void LLVMToSPIRV::transFPContract() {
25572573
}
25582574
}
25592575

2560-
bool LLVMToSPIRV::transOCLKernelMetadata() {
2576+
bool LLVMToSPIRV::transMetadata() {
2577+
if (!transOCLMetadata())
2578+
return false;
2579+
2580+
auto Model = getMemoryModel(*M);
2581+
if (Model != SPIRVMemoryModelKind::MemoryModelMax)
2582+
BM->setMemoryModel(static_cast<SPIRVMemoryModelKind>(Model));
2583+
2584+
return true;
2585+
}
2586+
2587+
bool LLVMToSPIRV::transOCLMetadata() {
25612588
for (auto &F : *M) {
25622589
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
25632590
continue;

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);

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)