Browse Source

[spirv] Emit Git commit info when debug information requested

If targeting Vulkan 1.1 & requesting debug information with -Zi,
two new OpModuleProcessed instruction will be emitted to contain
"dxc-commit-hash" & "dxc-commit-count".
Lei Zhang 7 years ago
parent
commit
d1531ece22

+ 7 - 3
tools/clang/include/clang/SPIRV/Structure.h

@@ -293,7 +293,7 @@ public:
   /// destructive; the module will be consumed and cleared after calling it.
   void take(InstBuilder *builder);
 
-  inline void setVersion(uint32_t version);
+  inline void useVulkan1p1();
   /// \brief Sets the id bound to the given bound.
   inline void setBound(uint32_t newBound);
 
@@ -328,6 +328,7 @@ public:
   inline uint32_t getExtInstSetId(llvm::StringRef setName);
 
 private:
+  bool isVulkan1p1;
   Header header; ///< SPIR-V module header.
   llvm::SetVector<spv::Capability> capabilities;
   llvm::SetVector<std::string> extensions;
@@ -453,10 +454,13 @@ TypeIdPair::TypeIdPair(const Type &ty, uint32_t id) : type(ty), resultId(id) {}
 // === Module inline implementations ===
 
 SPIRVModule::SPIRVModule()
-    : addressingModel(llvm::None), memoryModel(llvm::None),
+    : isVulkan1p1(false), addressingModel(llvm::None), memoryModel(llvm::None),
       shaderModelVersion(0), sourceFileNameId(0) {}
 
-void SPIRVModule::setVersion(uint32_t version) { header.version = version; }
+void SPIRVModule::useVulkan1p1() {
+  isVulkan1p1 = true;
+  header.version = 0x00010300u;
+}
 void SPIRVModule::setBound(uint32_t newBound) { header.bound = newBound; }
 
 void SPIRVModule::addCapability(spv::Capability cap) {

+ 1 - 1
tools/clang/lib/SPIRV/ModuleBuilder.cpp

@@ -28,7 +28,7 @@ ModuleBuilder::ModuleBuilder(SPIRVContext *C, FeatureManager *features,
 
   // Set the SPIR-V version if needed.
   if (featureManager && featureManager->getTargetEnv() == SPV_ENV_VULKAN_1_1)
-    theModule.setVersion(0x00010300);
+    theModule.useVulkan1p1();
 }
 
 std::vector<uint32_t> ModuleBuilder::takeModule() {

+ 19 - 0
tools/clang/lib/SPIRV/Structure.cpp

@@ -11,6 +11,15 @@
 
 #include "BlockReadableOrder.h"
 
+#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
+#include "clang/Basic/Version.h"
+#else
+namespace clang {
+uint32_t getGitCommitCount() { return 0; }
+const char *getGitCommitHash() { return "<unknown-hash>"; }
+} // namespace clang
+#endif // SUPPORT_QUERY_GIT_COMMIT_INFO
+
 namespace clang {
 namespace spirv {
 
@@ -353,6 +362,16 @@ void SPIRVModule::take(InstBuilder *builder) {
     }
   }
 
+  if (isVulkan1p1) {
+    std::string commitHash =
+        std::string("dxc-commit-hash: ") + clang::getGitCommitHash();
+    builder->opModuleProcessed(commitHash).x();
+
+    std::string commitCount = std::string("dxc-commit-count: ") +
+                              std::to_string(clang::getGitCommitCount());
+    builder->opModuleProcessed(commitCount).x();
+  }
+
   for (const auto &idDecorPair : decorations) {
     consumer(idDecorPair.second->withTargetId(idDecorPair.first));
   }

+ 6 - 0
tools/clang/test/CodeGenSPIRV/spirv.debug.commit.hlsl

@@ -0,0 +1,6 @@
+// Run: %dxc -T vs_6_0 -E main -fspv-target-env=vulkan1.1 -Zi
+
+// CHECK: OpModuleProcessed "dxc-commit-hash: {{\w+}}"
+// CHECK: OpModuleProcessed "dxc-commit-count: {{\d+}}"
+
+void main() { }

+ 5 - 0
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -1345,6 +1345,11 @@ TEST_F(FileTest, SpirvDebugOpLine) {
   runFileTest("spirv.debug.opline.hlsl");
 }
 
+TEST_F(FileTest, SpirvDebugDxcCommitInfo) {
+  useVulkan1p1();
+  runFileTest("spirv.debug.commit.hlsl");
+}
+
 TEST_F(FileTest, VulkanAttributeErrors) {
   runFileTest("vk.attribute.error.hlsl", Expect::Failure);
 }