Add GDB prettyprinters for a few more MLIR types.

Reviewed By: dblaikie, jpienaar

Differential Revision: https://reviews.llvm.org/D87159

GitOrigin-RevId: e9b38841619f20a6f4c8657880fd487083ba499a
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d3ac0a4..4b6af52 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,16 @@
   not
   )
 
+if ("mlir" IN_LIST LLVM_ENABLE_PROJECTS)
+  add_llvm_executable(check-gdb-mlir-support
+        llvm-prettyprinters/gdb/mlir-support.cpp
+  )
+  target_include_directories(check-gdb-mlir-support PRIVATE ${LLVM_EXTERNAL_MLIR_SOURCE_DIR}/include)
+  target_link_libraries(check-gdb-mlir-support PRIVATE MLIRIR)
+  list(APPEND DEBUGINFO_TEST_DEPS check-gdb-mlir-support)
+  set(MLIR_SOURCE_DIR  ${LLVM_EXTERNAL_MLIR_SOURCE_DIR})
+endif()
+
 if("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
   # llgdb-tests/asan.c and other asan* files.
   if(TARGET asan)
diff --git a/lit.cfg.py b/lit.cfg.py
index 4c45b72..0e43650 100644
--- a/lit.cfg.py
+++ b/lit.cfg.py
@@ -157,6 +157,6 @@
         if apple_lldb_vers < 1000:
             config.available_features.add('apple-lldb-pre-1000')
 
-llvm_config.feature_config([('--build-mode', {
-    'Debug|RelWithDebInfo': 'debug-info'
-})])
+llvm_config.feature_config(
+    [('--build-mode', {'Debug|RelWithDebInfo': 'debug-info'})]
+)
diff --git a/lit.site.cfg.py.in b/lit.site.cfg.py.in
index d5893f5..4ed49b8 100644
--- a/lit.site.cfg.py.in
+++ b/lit.site.cfg.py.in
@@ -20,6 +20,8 @@
 config.host_arch = "@HOST_ARCH@"
 config.is_msvc = lit.util.pythonize_bool("@MSVC@")
 
+config.mlir_src_root = "@MLIR_SOURCE_DIR@"
+
 config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
 
 config.python3_executable = "@Python3_EXECUTABLE@"
diff --git a/llvm-prettyprinters/gdb/lit.local.cfg b/llvm-prettyprinters/gdb/lit.local.cfg
index be053e0..a4200fb 100644
--- a/llvm-prettyprinters/gdb/lit.local.cfg
+++ b/llvm-prettyprinters/gdb/lit.local.cfg
@@ -4,6 +4,10 @@
 if 'native' not in config.available_features or lit.util.which('gdb') is None:
     config.unsupported = True
 
+if config.mlir_src_root:
+  config.substitutions.append(("%mlir_src_root", config.mlir_src_root))
+  config.available_features.add('mlir')
+
 config.suffixes = ['.gdb']
 
 
diff --git a/llvm-prettyprinters/gdb/llvm-support.cpp b/llvm-prettyprinters/gdb/llvm-support.cpp
index 04c84cd..a6e535b 100644
--- a/llvm-prettyprinters/gdb/llvm-support.cpp
+++ b/llvm-prettyprinters/gdb/llvm-support.cpp
@@ -53,8 +53,13 @@
   return Result;
 }();
 
-// Check expected instances to avoid compile errors.
-auto CheckExpectedValue = static_cast<bool>(ExpectedValue);
-auto CheckExpectedError = static_cast<bool>(ExpectedError);
-
-int main() { return 0; }
+int main() {
+  // Reference symbols that might otherwise be stripped.
+  ArrayRef[0];
+  MutableArrayRef[0];
+  !ExpectedValue;
+  !ExpectedError;
+  *OptionalValue;
+  *OptionalNone;
+  return 0;
+}
diff --git a/llvm-prettyprinters/gdb/mlir-support.cpp b/llvm-prettyprinters/gdb/mlir-support.cpp
new file mode 100644
index 0000000..548a9ed
--- /dev/null
+++ b/llvm-prettyprinters/gdb/mlir-support.cpp
@@ -0,0 +1,41 @@
+#include "mlir/IR/Identifier.h"
+#include "mlir/IR/Location.h"
+#include "mlir/IR/MLIRContext.h"
+#include "mlir/IR/OperationSupport.h"
+#include "mlir/IR/StandardTypes.h"
+
+mlir::MLIRContext Context;
+
+auto Identifier = mlir::Identifier::get("foo", &Context);
+mlir::OperationName OperationName("FooOp", &Context);
+mlir::Value Value({reinterpret_cast<void *>(0x8),
+                   mlir::Value::Kind::TrailingOpResult});
+
+mlir::Type Type(nullptr);
+mlir::Type IndexType = mlir::IndexType::get(&Context);
+mlir::Type IntegerType =
+    mlir::IntegerType::get(3, mlir::IntegerType::Unsigned, &Context);
+mlir::Type FloatType = mlir::Float32Type::get(&Context);
+mlir::Type MemRefType = mlir::MemRefType::get({4, 5}, FloatType);
+mlir::Type UnrankedMemRefType = mlir::UnrankedMemRefType::get(IntegerType, 6);
+mlir::Type VectorType = mlir::VectorType::get({1, 2}, FloatType);
+mlir::Type TupleType =
+    mlir::TupleType::get(mlir::TypeRange({IndexType, FloatType}), &Context);
+
+auto UnknownLoc = mlir::UnknownLoc::get(&Context);
+auto FileLineColLoc = mlir::FileLineColLoc::get("file", 7, 8, &Context);
+auto OpaqueLoc = mlir::OpaqueLoc::get<uintptr_t>(9, &Context);
+auto NameLoc = mlir::NameLoc::get(Identifier, &Context);
+auto CallSiteLoc = mlir::CallSiteLoc::get(FileLineColLoc, OpaqueLoc);
+auto FusedLoc = mlir::FusedLoc::get({FileLineColLoc, NameLoc}, &Context);
+
+mlir::Attribute UnitAttr = mlir::UnitAttr::get(&Context);
+mlir::Attribute FloatAttr = mlir::FloatAttr::get(FloatType, 1.0);
+mlir::Attribute IntegerAttr = mlir::IntegerAttr::get(IntegerType, 10);
+mlir::Attribute TypeAttr = mlir::TypeAttr::get(IndexType);
+mlir::Attribute ArrayAttr = mlir::ArrayAttr::get({UnitAttr}, &Context);
+mlir::Attribute StringAttr = mlir::StringAttr::get("foo", &Context);
+mlir::Attribute ElementsAttr = mlir::DenseElementsAttr::get(
+    VectorType.cast<mlir::ShapedType>(), llvm::ArrayRef<float>{2.0f, 3.0f});
+
+int main() { return 0; }
diff --git a/llvm-prettyprinters/gdb/mlir-support.gdb b/llvm-prettyprinters/gdb/mlir-support.gdb
new file mode 100644
index 0000000..2a82412
--- /dev/null
+++ b/llvm-prettyprinters/gdb/mlir-support.gdb
@@ -0,0 +1,112 @@
+# RUN: gdb -q -batch -n -iex 'source %mlir_src_root/utils/gdb-scripts/prettyprinters.py' -iex 'source %llvm_src_root/utils/gdb-scripts/prettyprinters.py' -x %s %llvm_tools_dir/check-gdb-mlir-support | FileCheck %s
+# REQUIRES: debug-info
+# REQUIRES: mlir
+
+break main
+run
+
+# CHECK: "foo"
+p Identifier
+
+# CHECK: "FooOp"
+p OperationName
+
+# CHECK: 0x8
+# CHECK: TrailingOpResult
+p Value
+
+# CHECK: impl = 0x0
+p Type
+
+# CHECK: cast<mlir::IndexType>
+p IndexType
+
+# CHECK: cast<mlir::IntegerType>
+# CHECK: width = 3
+# CHECK: Unsigned
+p IntegerType
+
+# CHECK: cast<mlir::Float32Type>
+p FloatType
+
+# CHECK: cast<mlir::MemRefType>
+# CHECK: shapeSize = 2
+# CHECK: shapeElements[0] = 4
+# CHECK: shapeElements[1] = 5
+p MemRefType
+
+# CHECK: cast<mlir::UnrankedMemRefType>
+# CHECK: memorySpace = 6
+p UnrankedMemRefType
+
+# CHECK: cast<mlir::VectorType>
+# CHECK: shapeSize = 2
+# CHECK: shapeElements[0] = 1
+# CHECK: shapeElements[1] = 2
+p VectorType
+
+# CHECK: cast<mlir::TupleType>
+# CHECK: numElements = 2
+# CHECK: elements[0]
+# CHECK: mlir::IndexType
+# CHECK: elements[1]
+# CHECK: mlir::Float32Type
+p TupleType
+
+# CHECK: cast<mlir::UnknownLoc>
+p UnknownLoc
+
+# CHECK: cast<mlir::FileLineColLoc>
+# CHECK: filename = "file"
+# CHECK: line = 7
+# CHECK: column = 8
+p FileLineColLoc
+
+# CHECK: cast<mlir::OpaqueLoc>
+# CHECK: underlyingLocation = 9
+p OpaqueLoc
+
+# CHECK: cast<mlir::NameLoc>
+# CHECK: name = "foo"
+# CHECK: mlir::UnknownLoc
+p NameLoc
+
+# CHECK: cast<mlir::CallSiteLoc>
+# CHECK: callee
+# CHECK: mlir::FileLineColLoc
+# CHECK: caller
+# CHECK: mlir::OpaqueLoc
+p CallSiteLoc
+
+# CHECK: cast<mlir::FusedLoc>
+# CHECK: numLocs = 2
+# CHECK: locs[0]
+# CHECK: mlir::FileLineColLoc
+# CHECK: locs[1]
+# CHECK: mlir::NameLoc
+p FusedLoc
+
+# CHECK: cast<mlir::UnitAttr>
+p UnitAttr
+
+# CHECK: cast<mlir::FloatAttr>
+p FloatAttr
+
+# CHECK: cast<mlir::IntegerAttr>
+p IntegerAttr
+
+# CHECK: cast<mlir::TypeAttr>
+# CHECK: mlir::IndexType
+p TypeAttr
+
+# CHECK: cast<mlir::ArrayAttr>
+# CHECK: llvm::ArrayRef of length 1
+# CHECK: mlir::UnitAttr
+p ArrayAttr
+
+# CHECK: cast<mlir::StringAttr>
+# CHECK: value = "foo"
+p StringAttr
+
+# CHECK: cast<mlir::DenseIntOrFPElementsAttr>
+p ElementsAttr