SpirvShaderDebugger: Implement globals, stub array types

Bug: b/148401179
Change-Id: Ic432d5e82793a1a4395365846d3c3c4205fdf7ba
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/44288
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/Pipeline/SpirvShaderDebugger.cpp b/src/Pipeline/SpirvShaderDebugger.cpp
index 214ef1c..9879278 100644
--- a/src/Pipeline/SpirvShaderDebugger.cpp
+++ b/src/Pipeline/SpirvShaderDebugger.cpp
@@ -106,6 +106,7 @@
 		Expression,
 		Function,
 		InlinedAt,
+		GlobalVariable,
 		LocalVariable,
 		Member,
 		Operation,
@@ -119,6 +120,7 @@
 
 		// Types
 		BasicType,
+		ArrayType,
 		VectorType,
 		FunctionType,
 		CompositeType,
@@ -146,6 +148,7 @@
 		case Object::Kind::Expression: return "Expression";
 		case Object::Kind::Function: return "Function";
 		case Object::Kind::InlinedAt: return "InlinedAt";
+		case Object::Kind::GlobalVariable: return "GlobalVariable";
 		case Object::Kind::LocalVariable: return "LocalVariable";
 		case Object::Kind::Member: return "Member";
 		case Object::Kind::Operation: return "Operation";
@@ -155,6 +158,7 @@
 		case Object::Kind::CompilationUnit: return "CompilationUnit";
 		case Object::Kind::LexicalBlock: return "LexicalBlock";
 		case Object::Kind::BasicType: return "BasicType";
+		case Object::Kind::ArrayType: return "ArrayType";
 		case Object::Kind::VectorType: return "VectorType";
 		case Object::Kind::FunctionType: return "FunctionType";
 		case Object::Kind::CompositeType: return "CompositeType";
@@ -227,6 +231,7 @@
 	static constexpr bool kindof(Kind kind)
 	{
 		return kind == Kind::BasicType ||
+		       kind == Kind::ArrayType ||
 		       kind == Kind::VectorType ||
 		       kind == Kind::FunctionType ||
 		       kind == Kind::CompositeType;
@@ -254,6 +259,12 @@
 	OpenCLDebugInfo100DebugBaseTypeAttributeEncoding encoding = OpenCLDebugInfo100Unspecified;
 };
 
+struct ArrayType : ObjectImpl<ArrayType, Type, Object::Kind::ArrayType>
+{
+	Type *base = nullptr;
+	std::vector<uint32_t> dimensions;
+};
+
 struct VectorType : ObjectImpl<VectorType, Type, Object::Kind::VectorType>
 {
 	Type *base = nullptr;
@@ -326,6 +337,19 @@
 	InlinedAt *inlinedAt = nullptr;
 };
 
+struct GlobalVariable : ObjectImpl<GlobalVariable, Object, Object::Kind::GlobalVariable>
+{
+	std::string name;
+	Type *type = nullptr;
+	Source *source = nullptr;
+	uint32_t line = 0;
+	uint32_t column = 0;
+	Scope *parent = nullptr;
+	std::string linkage;
+	sw::SpirvShader::Object::ID variable;
+	uint32_t flags = 0;  // OR'd from OpenCLDebugInfo100DebugInfoFlags
+};
+
 struct LocalVariable : ObjectImpl<LocalVariable, Object, Object::Kind::LocalVariable>
 {
 	static constexpr uint32_t NoArg = ~uint32_t(0);
@@ -889,6 +913,14 @@
 				type->encoding = static_cast<OpenCLDebugInfo100DebugBaseTypeAttributeEncoding>(insn.word(7));
 			});
 			break;
+		case OpenCLDebugInfo100DebugTypeArray:
+			defineOrEmit(insn, pass, [&](debug::ArrayType *type) {
+				type->base = get(debug::Type::ID(insn.word(5)));
+				auto &components = shader->getObject(insn.word(6));
+				ASSERT(components.kind == SpirvShader::Object::Kind::Constant);
+				type->dimensions = components.constantValue;
+			});
+			break;
 		case OpenCLDebugInfo100DebugTypeVector:
 			defineOrEmit(insn, pass, [&](debug::VectorType *type) {
 				type->base = get(debug::Type::ID(insn.word(5)));
@@ -939,6 +971,22 @@
 				member->flags = insn.word(13);
 			});
 			break;
+		case OpenCLDebugInfo100DebugGlobalVariable:
+			defineOrEmit(insn, pass, [&](debug::GlobalVariable *var) {
+				var->name = shader->getString(insn.word(5));
+				var->type = get(debug::Type::ID(insn.word(6)));
+				var->source = get(debug::Source::ID(insn.word(7)));
+				var->line = insn.word(8);
+				var->column = insn.word(9);
+				var->parent = get(debug::Scope::ID(insn.word(10)));
+				var->linkage = shader->getString(insn.word(11));
+				var->variable = insn.word(12);
+				var->flags = insn.word(13);
+				// static member declaration: word(14)
+
+				exposeVariable(shader, var->name.c_str(), &debug::Scope::Root, var->type, var->variable, state);
+			});
+			break;
 		case OpenCLDebugInfo100DebugFunction:
 			defineOrEmit(insn, pass, [&](debug::Function *func) {
 				func->name = shader->getString(insn.word(5));
@@ -1061,7 +1109,6 @@
 
 		case OpenCLDebugInfo100DebugTypePointer:
 		case OpenCLDebugInfo100DebugTypeQualifier:
-		case OpenCLDebugInfo100DebugTypeArray:
 		case OpenCLDebugInfo100DebugTypedef:
 		case OpenCLDebugInfo100DebugTypeEnum:
 		case OpenCLDebugInfo100DebugTypeInheritance:
@@ -1070,7 +1117,6 @@
 		case OpenCLDebugInfo100DebugTypeTemplateParameter:
 		case OpenCLDebugInfo100DebugTypeTemplateTemplateParameter:
 		case OpenCLDebugInfo100DebugTypeTemplateParameterPack:
-		case OpenCLDebugInfo100DebugGlobalVariable:
 		case OpenCLDebugInfo100DebugFunctionDeclaration:
 		case OpenCLDebugInfo100DebugLexicalBlockDiscriminator:
 		case OpenCLDebugInfo100DebugInlinedVariable:
@@ -1285,6 +1331,14 @@
 
 			return;
 		}
+		else if(auto ty = debug::cast<debug::ArrayType>(type))
+		{
+			// TODO(bclayton): Expose array types.
+		}
+		else
+		{
+			UNIMPLEMENTED("b/145351270: Debug type: %s", cstr(type->kind));
+		}
 	}
 
 	// No debug type information. Derive from SPIR-V.