Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions lib/SPIRV/PreprocessMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class PreprocessMetadata : public ModulePass {

bool runOnModule(Module &M) override;
void visit(Module *M);
void preprocessOCLMetadata(Module *M, SPIRVMDBuilder *B, SPIRVMDWalker *W);

static char ID;

Expand Down Expand Up @@ -98,48 +99,7 @@ void PreprocessMetadata::visit(Module *M) {
SPIRVMDBuilder B(*M);
SPIRVMDWalker W(*M);

unsigned CLVer = getOCLVersion(M, true);
if (CLVer != 0) { // Preprocess OpenCL-specific metadata
// !spirv.Source = !{!x}
// !{x} = !{i32 3, i32 102000}
B.addNamedMD(kSPIRVMD::Source)
.addOp()
.add(CLVer < kOCLVer::CL21 ? spv::SourceLanguageOpenCL_C
: spv::SourceLanguageOpenCL_CPP)
.add(CLVer)
.done();
if (EraseOCLMD)
B.eraseNamedMD(kSPIR2MD::OCLVer).eraseNamedMD(kSPIR2MD::SPIRVer);

// !spirv.MemoryModel = !{!x}
// !{x} = !{i32 1, i32 2}
Triple TT(M->getTargetTriple());
assert(isSupportedTriple(TT) && "Invalid triple");
B.addNamedMD(kSPIRVMD::MemoryModel)
.addOp()
.add(TT.isArch32Bit() ? spv::AddressingModelPhysical32
: spv::AddressingModelPhysical64)
.add(spv::MemoryModelOpenCL)
.done();

// Add source extensions
// !spirv.SourceExtension = !{!x, !y, ...}
// !x = {!"cl_khr_..."}
// !y = {!"cl_khr_..."}
auto Exts = getNamedMDAsStringSet(M, kSPIR2MD::Extensions);
if (!Exts.empty()) {
auto N = B.addNamedMD(kSPIRVMD::SourceExtension);
for (auto &I : Exts)
N.addOp().add(I).done();
}
if (EraseOCLMD)
B.eraseNamedMD(kSPIR2MD::Extensions).eraseNamedMD(kSPIR2MD::OptFeatures);

if (EraseOCLMD)
B.eraseNamedMD(kSPIR2MD::FPContract);
}

// The rest of metadata might come not only from OpenCL
preprocessOCLMetadata(M, &B, &W);

// Create metadata representing (empty so far) list
// of OpExecutionMode instructions
Expand Down Expand Up @@ -238,6 +198,51 @@ void PreprocessMetadata::visit(Module *M) {
}
}

void PreprocessMetadata::preprocessOCLMetadata(Module *M, SPIRVMDBuilder *B,
SPIRVMDWalker *W) {
unsigned CLVer = getOCLVersion(M, true);
if (CLVer == 0)
return;
// Preprocess OpenCL-specific metadata
// !spirv.Source = !{!x}
// !{x} = !{i32 3, i32 102000}
B->addNamedMD(kSPIRVMD::Source)
.addOp()
.add(CLVer < kOCLVer::CL21 ? spv::SourceLanguageOpenCL_C
: spv::SourceLanguageOpenCL_CPP)
.add(CLVer)
.done();
if (EraseOCLMD)
B->eraseNamedMD(kSPIR2MD::OCLVer).eraseNamedMD(kSPIR2MD::SPIRVer);

// !spirv.MemoryModel = !{!x}
// !{x} = !{i32 1, i32 2}
Triple TT(M->getTargetTriple());
assert(isSupportedTriple(TT) && "Invalid triple");
B->addNamedMD(kSPIRVMD::MemoryModel)
.addOp()
.add(TT.isArch32Bit() ? spv::AddressingModelPhysical32
: spv::AddressingModelPhysical64)
.add(spv::MemoryModelOpenCL)
.done();

// Add source extensions
// !spirv.SourceExtension = !{!x, !y, ...}
// !x = {!"cl_khr_..."}
// !y = {!"cl_khr_..."}
auto Exts = getNamedMDAsStringSet(M, kSPIR2MD::Extensions);
if (!Exts.empty()) {
auto N = B->addNamedMD(kSPIRVMD::SourceExtension);
for (auto &I : Exts)
N.addOp().add(I).done();
}
if (EraseOCLMD)
B->eraseNamedMD(kSPIR2MD::Extensions).eraseNamedMD(kSPIR2MD::OptFeatures);

if (EraseOCLMD)
B->eraseNamedMD(kSPIR2MD::FPContract);
}

} // namespace SPIRV

INITIALIZE_PASS(PreprocessMetadata, "preprocess-metadata",
Expand Down