Skip to content

Commit

Permalink
[mlir][capi] Add NameLoc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jpienaar committed Sep 1, 2021
1 parent f3645c7 commit f7bf8a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mlir/include/mlir-c/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions mlir/lib/CAPI/IR/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))));
}
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/CAPI/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f7bf8a8

Please sign in to comment.