[Mlir-commits] [mlir] 7bfdac0 - [MLIR] Expose LocationAttrs in the C API

Andrew Young llvmlistbot at llvm.org
Tue Jan 24 23:18:13 PST 2023


Author: Andrew Young
Date: 2023-01-24T23:15:00-08:00
New Revision: 7bfdac0e6dd8d9d9a2f8546dedea5d7ffcb5317e

URL: https://github.com/llvm/llvm-project/commit/7bfdac0e6dd8d9d9a2f8546dedea5d7ffcb5317e
DIFF: https://github.com/llvm/llvm-project/commit/7bfdac0e6dd8d9d9a2f8546dedea5d7ffcb5317e.diff

LOG: [MLIR] Expose LocationAttrs in the C API

This patch adds three functions to the C API:
- mlirAttributeIsALocation: returns true if the attribute is a LocationAttr,
  false otherwise.
- mlirLocationGetAttribute: returns the underlying LocationAttr of a Location.
- mlirLocationFromAttribute: gets a Location from a LocationAttr.

Reviewed By: mikeurbach, Mogball

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

Added: 
    

Modified: 
    mlir/include/mlir-c/BuiltinAttributes.h
    mlir/include/mlir-c/IR.h
    mlir/lib/CAPI/IR/BuiltinAttributes.cpp
    mlir/lib/CAPI/IR/IR.cpp
    mlir/test/CAPI/ir.c

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/BuiltinAttributes.h b/mlir/include/mlir-c/BuiltinAttributes.h
index 3b1ac466dd07b..2e62879396db2 100644
--- a/mlir/include/mlir-c/BuiltinAttributes.h
+++ b/mlir/include/mlir-c/BuiltinAttributes.h
@@ -25,6 +25,12 @@ extern "C" {
 /// Returns an empty attribute.
 MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeGetNull(void);
 
+//===----------------------------------------------------------------------===//
+// Location attribute.
+//===----------------------------------------------------------------------===//
+
+MLIR_CAPI_EXPORTED bool mlirAttributeIsALocation(MlirAttribute attr);
+
 //===----------------------------------------------------------------------===//
 // Affine map attribute.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 7817da2bd1056..a349eb9f39130 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -225,6 +225,14 @@ mlirDialectRegistryDestroy(MlirDialectRegistry registry);
 // Location API.
 //===----------------------------------------------------------------------===//
 
+/// Returns the underlying location attribute of this location.
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirLocationGetAttribute(MlirLocation location);
+
+/// Creates a location from a location attribute.
+MLIR_CAPI_EXPORTED MlirLocation
+mlirLocationFromAttribute(MlirAttribute attribute);
+
 /// Creates an File/Line/Column location owned by the given context.
 MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
     MlirContext context, MlirStringRef filename, unsigned line, unsigned col);

diff  --git a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
index b6ee4af79c878..66d291eddb65a 100644
--- a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
@@ -20,6 +20,14 @@ using namespace mlir;
 
 MlirAttribute mlirAttributeGetNull() { return {nullptr}; }
 
+//===----------------------------------------------------------------------===//
+// Location attribute.
+//===----------------------------------------------------------------------===//
+
+bool mlirAttributeIsALocation(MlirAttribute attr) {
+  return unwrap(attr).isa<LocationAttr>();
+}
+
 //===----------------------------------------------------------------------===//
 // Affine map attribute.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 68563a69c9f94..7d3479736c55d 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -145,6 +145,14 @@ void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
 // Location API.
 //===----------------------------------------------------------------------===//
 
+MlirAttribute mlirLocationGetAttribute(MlirLocation location) {
+  return wrap(LocationAttr(unwrap(location)));
+}
+
+MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {
+  return wrap(Location(unwrap(attribute).cast<LocationAttr>()));
+}
+
 MlirLocation mlirLocationFileLineColGet(MlirContext context,
                                         MlirStringRef filename, unsigned line,
                                         unsigned col) {

diff  --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 5b91883a3d8da..5f205c4ff5e2b 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -1305,6 +1305,11 @@ int printBuiltinAttributes(MlirContext ctx) {
           1e-6)
     return 23;
 
+  MlirLocation loc = mlirLocationUnknownGet(ctx);
+  MlirAttribute locAttr = mlirLocationGetAttribute(loc);
+  if (!mlirAttributeIsALocation(locAttr))
+    return 24;
+
   return 0;
 }
 
@@ -2159,6 +2164,9 @@ void testDiagnostics(void) {
   fprintf(stderr, "@test_diagnostics\n");
   MlirLocation unknownLoc = mlirLocationUnknownGet(ctx);
   mlirEmitError(unknownLoc, "test diagnostics");
+  MlirAttribute unknownAttr = mlirLocationGetAttribute(unknownLoc);
+  MlirLocation unknownClone = mlirLocationFromAttribute(unknownAttr);
+  mlirEmitError(unknownClone, "test clone");
   MlirLocation fileLineColLoc = mlirLocationFileLineColGet(
       ctx, mlirStringRefCreateFromCString("file.c"), 1, 2);
   mlirEmitError(fileLineColLoc, "test diagnostics");
@@ -2181,6 +2189,9 @@ void testDiagnostics(void) {
   // CHECK: processing diagnostic (userData: 42) <<
   // CHECK:   test diagnostics
   // CHECK:   loc(unknown)
+  // CHECK: processing diagnostic (userData: 42) <<
+  // CHECK:   test clone
+  // CHECK:   loc(unknown)
   // CHECK: >> end of diagnostic (userData: 42)
   // CHECK: processing diagnostic (userData: 42) <<
   // CHECK:   test diagnostics


        


More information about the Mlir-commits mailing list