Browse Source

[spirv] Output debug names for parameters/variables and blocks (#453)

Lei Zhang 8 years ago
parent
commit
056370e4ef

+ 5 - 4
tools/clang/include/clang/SPIRV/ModuleBuilder.h

@@ -15,6 +15,7 @@
 #include "clang/SPIRV/SPIRVContext.h"
 #include "clang/SPIRV/Structure.h"
 #include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/StringRef.h"
 
 namespace clang {
 namespace spirv {
@@ -42,18 +43,18 @@ public:
   /// exist at most one function under building. Returns the <result-id> for the
   /// function on success. Returns zero on failure.
   uint32_t beginFunction(uint32_t funcType, uint32_t returnType,
-                         std::string name = "");
+                         llvm::StringRef name = "");
 
   /// \brief Registers a function parameter of the given type onto the current
   /// function and returns its <result-id>.
-  uint32_t addFnParameter(uint32_t type);
+  uint32_t addFnParameter(uint32_t type, llvm::StringRef name = "");
 
   /// \brief Creates a local variable of the given value type in the current
   /// function and returns its <result-id>.
   ///
   /// The corresponding pointer type of the given value type will be constructed
   /// for the variable itself.
-  uint32_t addFnVariable(uint32_t valueType);
+  uint32_t addFnVariable(uint32_t valueType, llvm::StringRef name = "");
 
   /// \brief Ends building of the current function. Returns true of success,
   /// false on failure. All basic blocks constructed from the beginning or
@@ -62,7 +63,7 @@ public:
 
   /// \brief Creates a SPIR-V basic block. On success, returns the <label-id>
   /// for the basic block. On failure, returns zero.
-  uint32_t createBasicBlock();
+  uint32_t createBasicBlock(llvm::StringRef name = "");
 
   /// \brief Returns true if the current basic block inserting into is
   /// terminated.

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

@@ -30,6 +30,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/Optional.h"
+#include "llvm/ADT/StringRef.h"
 
 namespace clang {
 namespace spirv {
@@ -227,7 +228,7 @@ public:
                             llvm::ArrayRef<uint32_t> intefaces);
   inline void addExecutionMode(Instruction &&);
   // TODO: source code debug information
-  inline void addDebugName(uint32_t targetId, std::string name,
+  inline void addDebugName(uint32_t targetId, llvm::StringRef name,
                            llvm::Optional<uint32_t> memberIndex = llvm::None);
   inline void addDecoration(const Decoration &decoration, uint32_t targetId);
   inline void addType(const Type *type, uint32_t resultId);
@@ -364,9 +365,11 @@ void SPIRVModule::addEntryPoint(spv::ExecutionModel em, uint32_t targetId,
 void SPIRVModule::addExecutionMode(Instruction &&execMode) {
   executionModes.push_back(std::move(execMode));
 }
-void SPIRVModule::addDebugName(uint32_t targetId, std::string name,
+void SPIRVModule::addDebugName(uint32_t targetId, llvm::StringRef name,
                                llvm::Optional<uint32_t> memberIndex) {
-  debugNames.emplace_back(targetId, std::move(name), memberIndex);
+  if (!name.empty()) {
+    debugNames.emplace_back(targetId, name, memberIndex);
+  }
 }
 void SPIRVModule::addDecoration(const Decoration &decoration,
                                 uint32_t targetId) {

+ 2 - 2
tools/clang/lib/SPIRV/EmitSPIRVAction.cpp

@@ -173,7 +173,7 @@ public:
 
       if (decl->hasBody()) {
         // The entry basic block.
-        const uint32_t entryLabel = theBuilder.createBasicBlock();
+        const uint32_t entryLabel = theBuilder.createBasicBlock("bb.entry");
         theBuilder.setInsertPoint(entryLabel);
 
         // Process all statments in the body.
@@ -203,7 +203,7 @@ public:
       const uint32_t ptrType = theBuilder.getPointerType(
           typeTranslator.translateType(decl->getType()),
           spv::StorageClass::Function);
-      const uint32_t varId = theBuilder.addFnVariable(ptrType);
+      const uint32_t varId = theBuilder.addFnVariable(ptrType, decl->getName());
       declIdMapper.registerDeclResultId(decl, varId);
     } else {
       // TODO: handle global variables

+ 8 - 9
tools/clang/lib/SPIRV/ModuleBuilder.cpp

@@ -37,7 +37,7 @@ std::vector<uint32_t> ModuleBuilder::takeModule() {
 }
 
 uint32_t ModuleBuilder::beginFunction(uint32_t funcType, uint32_t returnType,
-                                      std::string funcName) {
+                                      llvm::StringRef funcName) {
   if (theFunction) {
     assert(false && "found nested function");
     return 0;
@@ -47,31 +47,29 @@ uint32_t ModuleBuilder::beginFunction(uint32_t funcType, uint32_t returnType,
 
   theFunction = llvm::make_unique<Function>(
       returnType, fId, spv::FunctionControlMask::MaskNone, funcType);
-
-  // Add debug name for the function (OpName ...)
-  if (!funcName.empty()) {
-    theModule.addDebugName(fId, std::move(funcName));
-  }
+  theModule.addDebugName(fId, funcName);
 
   return fId;
 }
 
-uint32_t ModuleBuilder::addFnParameter(uint32_t type) {
+uint32_t ModuleBuilder::addFnParameter(uint32_t type, llvm::StringRef name) {
   assert(theFunction && "found detached parameter");
 
   const uint32_t pointerType =
       getPointerType(type, spv::StorageClass::Function);
   const uint32_t paramId = theContext.takeNextId();
   theFunction->addParameter(pointerType, paramId);
+  theModule.addDebugName(paramId, name);
 
   return paramId;
 }
 
-uint32_t ModuleBuilder::addFnVariable(uint32_t type) {
+uint32_t ModuleBuilder::addFnVariable(uint32_t type, llvm::StringRef name) {
   assert(theFunction && "found detached local variable");
 
   const uint32_t varId = theContext.takeNextId();
   theFunction->addVariable(type, varId);
+  theModule.addDebugName(varId, name);
   return varId;
 }
 
@@ -97,7 +95,7 @@ bool ModuleBuilder::endFunction() {
   return true;
 }
 
-uint32_t ModuleBuilder::createBasicBlock() {
+uint32_t ModuleBuilder::createBasicBlock(llvm::StringRef name) {
   if (theFunction == nullptr) {
     assert(false && "found detached basic block");
     return 0;
@@ -105,6 +103,7 @@ uint32_t ModuleBuilder::createBasicBlock() {
 
   const uint32_t labelId = theContext.takeNextId();
   basicBlocks[labelId] = llvm::make_unique<BasicBlock>(labelId);
+  theModule.addDebugName(labelId, name);
 
   return labelId;
 }

+ 2 - 1
tools/clang/test/CodeGenSPIRV/constant-ps.hlsl2spv

@@ -15,6 +15,7 @@ float4 main(): SV_TARGET
 // OpEntryPoint Fragment %main "main" %4
 // OpExecutionMode %main OriginUpperLeft
 // OpName %main "main"
+// OpName %bb_entry "bb.entry"
 // OpDecorate %4 Location 0
 // %float = OpTypeFloat 32
 // %v4float = OpTypeVector %float 4
@@ -28,7 +29,7 @@ float4 main(): SV_TARGET
 // %13 = OpConstantComposite %v4float %float_1 %float_2 %float_3_5 %float_4_7
 // %4 = OpVariable %_ptr_Output_v4float Output
 // %main = OpFunction %void None %6
-// %8 = OpLabel
+// %bb_entry = OpLabel
 // OpStore %4 %13
 // OpReturn
 // OpFunctionEnd

+ 3 - 2
tools/clang/test/CodeGenSPIRV/empty-void-main.hlsl2spv

@@ -15,9 +15,10 @@ void main()
 // OpEntryPoint Fragment %main "main"
 // OpExecutionMode %main OriginUpperLeft
 // OpName %main "main"
+// OpName %bb_entry "bb.entry"
 // %void = OpTypeVoid
 // %2 = OpTypeFunction %void
 // %main = OpFunction %void None %2
-// %4 = OpLabel
+// %bb_entry = OpLabel
 // OpReturn
-// OpFunctionEnd
+// OpFunctionEnd

+ 2 - 1
tools/clang/test/CodeGenSPIRV/passthru-ps.hlsl2spv

@@ -15,6 +15,7 @@ float4 main(float4 input: COLOR): SV_TARGET
 // OpEntryPoint Fragment %main "main" %6 %4
 // OpExecutionMode %main OriginUpperLeft
 // OpName %main "main"
+// OpName %bb_entry "bb.entry"
 // OpDecorate %6 Location 0
 // OpDecorate %4 Location 0
 // %float = OpTypeFloat 32
@@ -26,7 +27,7 @@ float4 main(float4 input: COLOR): SV_TARGET
 // %4 = OpVariable %_ptr_Output_v4float Output
 // %6 = OpVariable %_ptr_Input_v4float Input
 // %main = OpFunction %void None %8
-// %10 = OpLabel
+// %bb_entry = OpLabel
 // %11 = OpLoad %v4float %6
 // OpStore %4 %11
 // OpReturn

+ 8 - 6
tools/clang/test/CodeGenSPIRV/passthru-vs.hlsl2spv

@@ -23,6 +23,8 @@ PSInput VSmain(float4 position: POSITION, float4 color: COLOR) {
 // OpMemoryModel Logical GLSL450
 // OpEntryPoint Vertex %VSmain "VSmain" %gl_Position %7 %8 %5
 // OpName %VSmain "VSmain"
+// OpName %bb_entry "bb.entry"
+// OpName %result "result"
 // OpDecorate %gl_Position BuiltIn Position
 // OpDecorate %7 Location 0
 // OpDecorate %8 Location 1
@@ -44,18 +46,18 @@ PSInput VSmain(float4 position: POSITION, float4 color: COLOR) {
 // %7 = OpVariable %_ptr_Input_v4float Input
 // %8 = OpVariable %_ptr_Input_v4float Input
 // %VSmain = OpFunction %void None %10
-// %12 = OpLabel
-// %15 = OpVariable %_ptr_Function__struct_13 Function
-// %19 = OpAccessChain %_ptr_Function_v4float %15 %int_0
+// %bb_entry = OpLabel
+// %result = OpVariable %_ptr_Function__struct_13 Function
+// %19 = OpAccessChain %_ptr_Function_v4float %result %int_0
 // %20 = OpLoad %v4float %7
 // OpStore %19 %20
-// %22 = OpAccessChain %_ptr_Function_v4float %15 %int_1
+// %22 = OpAccessChain %_ptr_Function_v4float %result %int_1
 // %23 = OpLoad %v4float %8
 // OpStore %22 %23
-// %24 = OpAccessChain %_ptr_Function_v4float %15 %int_0
+// %24 = OpAccessChain %_ptr_Function_v4float %result %int_0
 // %25 = OpLoad %v4float %24
 // OpStore %gl_Position %25
-// %26 = OpAccessChain %_ptr_Function_v4float %15 %int_1
+// %26 = OpAccessChain %_ptr_Function_v4float %result %int_1
 // %27 = OpLoad %v4float %26
 // OpStore %5 %27
 // OpReturn