Skip to content

Commit f7bf8a8

Browse files
committed
[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
1 parent f3645c7 commit f7bf8a8

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

mlir/include/mlir-c/IR.h

+7
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
162162
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
163163
MlirLocation caller);
164164

165+
/// Creates a name location owned by the given context. Providing null location
166+
/// for childLoc is allowed and if childLoc is null location, then the behavior
167+
/// is the same as having unknown child location.
168+
MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context,
169+
MlirStringRef name,
170+
MlirLocation childLoc);
171+
165172
/// Creates a location with unknown position owned by the given context.
166173
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);
167174

mlir/lib/CAPI/IR/IR.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/IR/Attributes.h"
1616
#include "mlir/IR/BuiltinOps.h"
1717
#include "mlir/IR/Dialect.h"
18+
#include "mlir/IR/Location.h"
1819
#include "mlir/IR/Operation.h"
1920
#include "mlir/IR/Types.h"
2021
#include "mlir/IR/Verifier.h"
@@ -131,6 +132,15 @@ MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
131132
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
132133
}
133134

135+
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
136+
MlirLocation childLoc) {
137+
if (mlirLocationIsNull(childLoc))
138+
return wrap(
139+
Location(NameLoc::get(Identifier::get(unwrap(name), unwrap(context)))));
140+
return wrap(Location(NameLoc::get(
141+
Identifier::get(unwrap(name), unwrap(context)), unwrap(childLoc))));
142+
}
143+
134144
MlirLocation mlirLocationUnknownGet(MlirContext context) {
135145
return wrap(Location(UnknownLoc::get(unwrap(context))));
136146
}

mlir/test/CAPI/ir.c

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mlir-c/Dialect/Standard.h"
2020
#include "mlir-c/IntegerSet.h"
2121
#include "mlir-c/Registration.h"
22+
#include "mlir-c/Support.h"
2223

2324
#include <assert.h>
2425
#include <inttypes.h>
@@ -1703,6 +1704,10 @@ void testDiagnostics() {
17031704
ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3),
17041705
fileLineColLoc);
17051706
mlirEmitError(callSiteLoc, "test diagnostics");
1707+
MlirLocation null = {0};
1708+
MlirLocation nameLoc =
1709+
mlirLocationNameGet(ctx, mlirStringRefCreateFromCString("named"), null);
1710+
mlirEmitError(nameLoc, "test diagnostics");
17061711
mlirContextDetachDiagnosticHandler(ctx, id);
17071712
mlirEmitError(unknownLoc, "more test diagnostics");
17081713
// CHECK-LABEL: @test_diagnostics
@@ -1718,6 +1723,10 @@ void testDiagnostics() {
17181723
// CHECK: test diagnostics
17191724
// CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2))
17201725
// CHECK: >> end of diagnostic (userData: 42)
1726+
// CHECK: processing diagnostic (userData: 42) <<
1727+
// CHECK: test diagnostics
1728+
// CHECK: loc("named")
1729+
// CHECK: >> end of diagnostic (userData: 42)
17211730
// CHECK: deleting user data (userData: 42)
17221731
// CHECK-NOT: processing diagnostic
17231732
// CHECK: more test diagnostics

0 commit comments

Comments
 (0)