Eliminate SpirvShaderEnumNames.cpp

It only contained the now trivial OpcodeName() implementation. Since the
SpirvShaderInstructions.cpp file already contains other SPIR-V opcode
utility functions, move it there.

Also make this function return a pointer to a constant char string,
to avoid needless std::string construction.

Bug: b/145351270
Change-Id: Iacfc887566b0a9057f37391ad52d43cdca912308
Signed-off-by: Nicolas Capens <capn@google.com>
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51448
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Pipeline/BUILD.gn b/src/Pipeline/BUILD.gn
index 998cb47..d579689 100644
--- a/src/Pipeline/BUILD.gn
+++ b/src/Pipeline/BUILD.gn
@@ -43,10 +43,10 @@
     "SpirvShaderArithmetic.cpp",
     "SpirvShaderControlFlow.cpp",
     "SpirvShaderDebugger.cpp",
-    "SpirvShaderEnumNames.cpp",
     "SpirvShaderGLSLstd450.cpp",
     "SpirvShaderGroup.cpp",
     "SpirvShaderImage.cpp",
+    "SpirvShaderInstructions.cpp",
     "SpirvShaderMemory.cpp",
     "SpirvShaderSampling.cpp",
     "SpirvShaderSpec.cpp",
diff --git a/src/Pipeline/CMakeLists.txt b/src/Pipeline/CMakeLists.txt
index 7b92eff..ba8c1f2 100644
--- a/src/Pipeline/CMakeLists.txt
+++ b/src/Pipeline/CMakeLists.txt
@@ -38,7 +38,6 @@
     SpirvShaderArithmetic.cpp
     SpirvShaderControlFlow.cpp
     SpirvShaderDebugger.cpp
-    SpirvShaderEnumNames.cpp
     SpirvShaderGLSLstd450.cpp
     SpirvShaderGroup.cpp
     SpirvShaderImage.cpp
diff --git a/src/Pipeline/SpirvShader.cpp b/src/Pipeline/SpirvShader.cpp
index 0c8ab83..581511c 100644
--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -483,7 +483,7 @@
 			case spv::OpFunctionParameter:
 				// These should have all been removed by preprocessing passes. If we see them here,
 				// our assumptions are wrong and we will probably generate wrong code.
-				UNREACHABLE("%s should have already been lowered.", OpcodeName(opcode).c_str());
+				UNREACHABLE("%s should have already been lowered.", OpcodeName(opcode));
 				break;
 
 			case spv::OpFunctionCall:
@@ -743,7 +743,7 @@
 			}
 
 			default:
-				UNSUPPORTED("%s", OpcodeName(opcode).c_str());
+				UNSUPPORTED("%s", OpcodeName(opcode));
 		}
 	}
 
@@ -987,7 +987,7 @@
 			return 1;
 
 		default:
-			UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str());
+			UNREACHABLE("%s", OpcodeName(insn.opcode()));
 			return 0;
 	}
 }
@@ -1110,7 +1110,7 @@
 				d->InsideMatrix = true;
 				break;
 			default:
-				UNREACHABLE("%s", OpcodeName(type.definition.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(type.definition.opcode()));
 		}
 	}
 }
@@ -1219,7 +1219,7 @@
 				break;
 			}
 			default:
-				UNREACHABLE("%s", OpcodeName(type.definition.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(type.definition.opcode()));
 		}
 	}
 
@@ -1300,7 +1300,7 @@
 			}
 
 			default:
-				UNREACHABLE("%s", OpcodeName(type.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(type.opcode()));
 		}
 	}
 
@@ -1346,7 +1346,7 @@
 			}
 
 			default:
-				UNREACHABLE("%s", OpcodeName(type.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(type.opcode()));
 		}
 	}
 
@@ -2028,7 +2028,7 @@
 			return EmitArrayLength(insn, state);
 
 		default:
-			UNREACHABLE("%s", OpcodeName(opcode).c_str());
+			UNREACHABLE("%s", OpcodeName(opcode));
 			break;
 	}
 
@@ -2328,7 +2328,7 @@
 					v = ExchangeAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder);
 					break;
 				default:
-					UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str());
+					UNREACHABLE("%s", OpcodeName(insn.opcode()));
 					break;
 			}
 			result = Insert(result, v, j);
diff --git a/src/Pipeline/SpirvShader.hpp b/src/Pipeline/SpirvShader.hpp
index ef1a460..826aff3 100644
--- a/src/Pipeline/SpirvShader.hpp
+++ b/src/Pipeline/SpirvShader.hpp
@@ -1250,8 +1250,7 @@
 	void WriteCFGGraphVizDotFile(const char *path) const;
 
 	// OpcodeName() returns the name of the opcode op.
-	// If NDEBUG is defined, then OpcodeName() will only return the numerical code.
-	static std::string OpcodeName(spv::Op op);
+	static const char *OpcodeName(spv::Op op);
 	static std::memory_order MemoryOrder(spv::MemorySemanticsMask memorySemantics);
 
 	// IsStatement() returns true if the given opcode actually performs
diff --git a/src/Pipeline/SpirvShaderArithmetic.cpp b/src/Pipeline/SpirvShaderArithmetic.cpp
index e730505..61d7bcc 100644
--- a/src/Pipeline/SpirvShaderArithmetic.cpp
+++ b/src/Pipeline/SpirvShaderArithmetic.cpp
@@ -299,7 +299,7 @@
 				break;
 			}
 			default:
-				UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(insn.opcode()));
 		}
 	}
 
@@ -502,7 +502,7 @@
 				dst.move(i + lhsType.componentCount, CmpLT(lhs.UInt(i), rhs.UInt(i)) >> 31);
 				break;
 			default:
-				UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str());
+				UNREACHABLE("%s", OpcodeName(insn.opcode()));
 		}
 	}
 
diff --git a/src/Pipeline/SpirvShaderControlFlow.cpp b/src/Pipeline/SpirvShaderControlFlow.cpp
index fbe96698..826d9ff 100644
--- a/src/Pipeline/SpirvShaderControlFlow.cpp
+++ b/src/Pipeline/SpirvShaderControlFlow.cpp
@@ -605,7 +605,7 @@
 
 			if(blockInsn.opcode() != wrapOpKill[insnNumber++])
 			{
-				UNIMPLEMENTED("b/141246700: Function block instruction %d : %s", insnNumber - 1, OpcodeName(blockInsn.opcode()).c_str());  // FIXME(b/141246700)
+				UNIMPLEMENTED("b/141246700: Function block instruction %d : %s", insnNumber - 1, OpcodeName(blockInsn.opcode()));  // FIXME(b/141246700)
 				return EmitResult::Continue;
 			}
 
diff --git a/src/Pipeline/SpirvShaderDebug.hpp b/src/Pipeline/SpirvShaderDebug.hpp
index 959c542..6aa0c7c 100644
--- a/src/Pipeline/SpirvShaderDebug.hpp
+++ b/src/Pipeline/SpirvShaderDebug.hpp
@@ -15,6 +15,8 @@
 #ifndef sw_SpirvShaderDebug_hpp
 #define sw_SpirvShaderDebug_hpp
 
+#include "SpirvShader.hpp"
+
 // Enable this to print verbose debug messages as each SPIR-V instructon is
 // executed. Very handy for performing text diffs when the thread count is
 // reduced to 1 and execution is deterministic.
diff --git a/src/Pipeline/SpirvShaderDebugger.cpp b/src/Pipeline/SpirvShaderDebugger.cpp
index c6e93c5..4120b1f 100644
--- a/src/Pipeline/SpirvShaderDebugger.cpp
+++ b/src/Pipeline/SpirvShaderDebugger.cpp
@@ -1991,7 +1991,7 @@
 	auto entryIt = entries.find(objId);
 	ASSERT_MSG(entryIt != entries.end(), "Missing shadow entry for object %%%d (%s)",
 	           (int)objId.value(),
-	           OpcodeName(state->debugger->shader->getObject(objId).opcode()).c_str());
+	           OpcodeName(state->debugger->shader->getObject(objId).opcode()));
 	auto &entry = entryIt->second;
 	auto data = &state->shadow[entry.offset];
 	return Memory{ data };
diff --git a/src/Pipeline/SpirvShaderEnumNames.cpp b/src/Pipeline/SpirvShaderEnumNames.cpp
deleted file mode 100644
index 7956995..0000000
--- a/src/Pipeline/SpirvShaderEnumNames.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// This file contains code used to aid debugging.
-
-#include "SpirvShader.hpp"
-
-#include "spirv-tools/libspirv.h"
-#include "spirv/unified1/spirv.h"
-
-namespace sw {
-
-std::string SpirvShader::OpcodeName(spv::Op op)
-{
-	return spvOpcodeString(static_cast<SpvOp>(op));
-}
-
-}  // namespace sw
diff --git a/src/Pipeline/SpirvShaderGroup.cpp b/src/Pipeline/SpirvShaderGroup.cpp
index 19e997f..3750b38 100644
--- a/src/Pipeline/SpirvShaderGroup.cpp
+++ b/src/Pipeline/SpirvShaderGroup.cpp
@@ -70,7 +70,7 @@
 				}
 				default:
 					UNSUPPORTED("EmitGroupNonUniform op: %s Group operation: %d",
-					            SpirvShader::OpcodeName(type.opcode()).c_str(), insn.word(4));
+					            SpirvShader::OpcodeName(type.opcode()), insn.word(4));
 			}
 		}
 	}
@@ -449,7 +449,7 @@
 			break;
 
 		default:
-			UNSUPPORTED("EmitGroupNonUniform op: %s", OpcodeName(type.opcode()).c_str());
+			UNSUPPORTED("EmitGroupNonUniform op: %s", OpcodeName(type.opcode()));
 	}
 	return EmitResult::Continue;
 }
diff --git a/src/Pipeline/SpirvShaderInstructions.cpp b/src/Pipeline/SpirvShaderInstructions.cpp
index 18cced1..02ce0d2 100644
--- a/src/Pipeline/SpirvShaderInstructions.cpp
+++ b/src/Pipeline/SpirvShaderInstructions.cpp
@@ -14,6 +14,8 @@
 
 #include "SpirvShader.hpp"
 
+#include "spirv-tools/libspirv.h"
+
 #include <spirv/unified1/spirv.hpp>
 
 #define CONCAT(a, b) a##b
@@ -45,6 +47,11 @@
 
 namespace sw {
 
+const char *SpirvShader::OpcodeName(spv::Op op)
+{
+	return spvOpcodeString(op);
+}
+
 bool SpirvShader::IsStatement(spv::Op op)
 {
 	switch(op)
diff --git a/src/Pipeline/SpirvShaderMemory.cpp b/src/Pipeline/SpirvShaderMemory.cpp
index ec82813..6722b0b 100644
--- a/src/Pipeline/SpirvShaderMemory.cpp
+++ b/src/Pipeline/SpirvShaderMemory.cpp
@@ -344,7 +344,7 @@
 			break;
 		}
 		default:
-			UNREACHABLE("%s", OpcodeName(type.opcode()).c_str());
+			UNREACHABLE("%s", OpcodeName(type.opcode()));
 	}
 }
 
diff --git a/src/Pipeline/SpirvShaderSpec.cpp b/src/Pipeline/SpirvShaderSpec.cpp
index fd96185..9225e9d 100644
--- a/src/Pipeline/SpirvShaderSpec.cpp
+++ b/src/Pipeline/SpirvShaderSpec.cpp
@@ -149,7 +149,7 @@
 			// Other spec constant ops are possible, but require capabilities that are
 			// not exposed in our Vulkan implementation (eg Kernel), so we should never
 			// get here for correct shaders.
-			UNSUPPORTED("EvalSpecConstantOp op: %s", OpcodeName(opcode).c_str());
+			UNSUPPORTED("EvalSpecConstantOp op: %s", OpcodeName(opcode));
 	}
 }
 
@@ -198,7 +198,7 @@
 				break;
 			}
 			default:
-				UNREACHABLE("EvalSpecConstantUnaryOp op: %s", OpcodeName(opcode).c_str());
+				UNREACHABLE("EvalSpecConstantUnaryOp op: %s", OpcodeName(opcode));
 		}
 	}
 }
@@ -306,7 +306,7 @@
 				v = static_cast<int32_t>(l) >= static_cast<int32_t>(r) ? ~0u : 0u;
 				break;
 			default:
-				UNREACHABLE("EvalSpecConstantBinaryOp op: %s", OpcodeName(opcode).c_str());
+				UNREACHABLE("EvalSpecConstantBinaryOp op: %s", OpcodeName(opcode));
 		}
 	}
 }