From e8c996c8b48d0906d70e35fb209c007ce49894ed Mon Sep 17 00:00:00 2001 From: nrudenko Date: Thu, 30 Apr 2020 12:08:45 +0300 Subject: [PATCH] Move preprocessing OCL metadata to separate function --- lib/SPIRV/PreprocessMetadata.cpp | 89 +++++++++++++++++--------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/lib/SPIRV/PreprocessMetadata.cpp b/lib/SPIRV/PreprocessMetadata.cpp index 46a3837939..5c491ba210 100644 --- a/lib/SPIRV/PreprocessMetadata.cpp +++ b/lib/SPIRV/PreprocessMetadata.cpp @@ -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; @@ -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 @@ -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",