[Mlir-commits] [mlir] f7bf8a8 - [mlir][capi] Add NameLoc
Jacques Pienaar
llvmlistbot at llvm.org
Wed Sep 1 16:16:49 PDT 2021
Author: Jacques Pienaar
Date: 2021-09-01T16:16:35-07:00
New Revision: f7bf8a865863ca18f5d566832049982a361fd7f8
URL: https://github.com/llvm/llvm-project/commit/f7bf8a865863ca18f5d566832049982a361fd7f8
DIFF: https://github.com/llvm/llvm-project/commit/f7bf8a865863ca18f5d566832049982a361fd7f8.diff
LOG: [mlir][capi] Add NameLoc
Add method to get NameLoc. Treat null child location as unknown to avoid
needing to create UnknownLoc in C API where child loc is not needed.
Differential Revision: https://reviews.llvm.org/D108678
Added:
Modified:
mlir/include/mlir-c/IR.h
mlir/lib/CAPI/IR/IR.cpp
mlir/test/CAPI/ir.c
Removed:
################################################################################
diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index ebc3ada600fde..d875e3807bcc6 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -162,6 +162,13 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
MlirLocation caller);
+/// Creates a name location owned by the given context. Providing null location
+/// for childLoc is allowed and if childLoc is null location, then the behavior
+/// is the same as having unknown child location.
+MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context,
+ MlirStringRef name,
+ MlirLocation childLoc);
+
/// Creates a location with unknown position owned by the given context.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);
diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 68037f0afe9c3..bbadc351d9779 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -15,6 +15,7 @@
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
+#include "mlir/IR/Location.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Verifier.h"
@@ -131,6 +132,15 @@ MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
}
+MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
+ MlirLocation childLoc) {
+ if (mlirLocationIsNull(childLoc))
+ return wrap(
+ Location(NameLoc::get(Identifier::get(unwrap(name), unwrap(context)))));
+ return wrap(Location(NameLoc::get(
+ Identifier::get(unwrap(name), unwrap(context)), unwrap(childLoc))));
+}
+
MlirLocation mlirLocationUnknownGet(MlirContext context) {
return wrap(Location(UnknownLoc::get(unwrap(context))));
}
diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 1acd4b22bbf48..d1bdb66f26722 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -19,6 +19,7 @@
#include "mlir-c/Dialect/Standard.h"
#include "mlir-c/IntegerSet.h"
#include "mlir-c/Registration.h"
+#include "mlir-c/Support.h"
#include <assert.h>
#include <inttypes.h>
@@ -1703,6 +1704,10 @@ void testDiagnostics() {
ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3),
fileLineColLoc);
mlirEmitError(callSiteLoc, "test diagnostics");
+ MlirLocation null = {0};
+ MlirLocation nameLoc =
+ mlirLocationNameGet(ctx, mlirStringRefCreateFromCString("named"), null);
+ mlirEmitError(nameLoc, "test diagnostics");
mlirContextDetachDiagnosticHandler(ctx, id);
mlirEmitError(unknownLoc, "more test diagnostics");
// CHECK-LABEL: @test_diagnostics
@@ -1718,6 +1723,10 @@ void testDiagnostics() {
// CHECK: test diagnostics
// CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2))
// CHECK: >> end of diagnostic (userData: 42)
+ // CHECK: processing diagnostic (userData: 42) <<
+ // CHECK: test diagnostics
+ // CHECK: loc("named")
+ // CHECK: >> end of diagnostic (userData: 42)
// CHECK: deleting user data (userData: 42)
// CHECK-NOT: processing diagnostic
// CHECK: more test diagnostics
More information about the Mlir-commits
mailing list