diff --git a/mlir/include/mlir-c/BuiltinAttributes.h b/mlir/include/mlir-c/BuiltinAttributes.h --- a/mlir/include/mlir-c/BuiltinAttributes.h +++ b/mlir/include/mlir-c/BuiltinAttributes.h @@ -25,6 +25,12 @@ /// 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 --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -225,6 +225,14 @@ // 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 --- a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp +++ b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp @@ -20,6 +20,14 @@ MlirAttribute mlirAttributeGetNull() { return {nullptr}; } +//===----------------------------------------------------------------------===// +// Location attribute. +//===----------------------------------------------------------------------===// + +bool mlirAttributeIsALocation(MlirAttribute attr) { + return unwrap(attr).isa(); +} + //===----------------------------------------------------------------------===// // Affine map attribute. //===----------------------------------------------------------------------===// diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -145,6 +145,14 @@ // Location API. //===----------------------------------------------------------------------===// +MlirAttribute mlirLocationGetAttribute(MlirLocation location) { + return wrap(LocationAttr(unwrap(location))); +} + +MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) { + return wrap(Location(unwrap(attribute).cast())); +} + MlirLocation mlirLocationFileLineColGet(MlirContext context, MlirStringRef filename, unsigned line, unsigned col) { diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -1305,6 +1305,11 @@ 1e-6) return 23; + MlirLocation loc = mlirLocationUnknownGet(ctx); + MlirAttribute locAttr = mlirLocationGetAttribute(loc); + if (!mlirAttributeIsALocation(locAttr)) + return 24; + return 0; } @@ -2159,6 +2164,9 @@ 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 @@ // 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