Skip to content

Commit 54986ba

Browse files
committed
[mlir][CAPI][python] bind CallSiteLoc, FileLineColRange, FusedLoc, NameLoc, OpaqueLoc
1 parent 7612dcc commit 54986ba

File tree

3 files changed

+178
-2
lines changed

3 files changed

+178
-2
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,96 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(
261261
MlirContext context, MlirStringRef filename, unsigned start_line,
262262
unsigned start_col, unsigned end_line, unsigned end_col);
263263

264+
/// Getter for filename of FileLineColRange.
265+
MLIR_CAPI_EXPORTED MlirIdentifier
266+
mlirLocationFileLineColRangeGetFilename(MlirLocation location);
267+
268+
/// Getter for start_line of FileLineColRange.
269+
MLIR_CAPI_EXPORTED unsigned
270+
mlirLocationFileLineColRangeGetStartLine(MlirLocation location);
271+
272+
/// Getter for start_column of FileLineColRange.
273+
MLIR_CAPI_EXPORTED unsigned
274+
mlirLocationFileLineColRangeGetStartColumn(MlirLocation location);
275+
276+
/// Getter for end_line of FileLineColRange.
277+
MLIR_CAPI_EXPORTED unsigned
278+
mlirLocationFileLineColRangeGetEndLine(MlirLocation location);
279+
280+
/// Getter for end_column of FileLineColRange.
281+
MLIR_CAPI_EXPORTED unsigned
282+
mlirLocationFileLineColRangeGetEndColumn(MlirLocation location);
283+
284+
/// TypeID Getter for FileLineColRange.
285+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFileLineColRangeGetTypeID(void);
286+
287+
/// Checks whether the given location is an FileLineColRange.
288+
MLIR_CAPI_EXPORTED bool mlirLocationIsAFileLineColRange(MlirLocation location);
289+
264290
/// Creates a call site location with a callee and a caller.
265291
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
266292
MlirLocation caller);
267293

294+
/// Getter for callee of CallSite.
295+
MLIR_CAPI_EXPORTED MlirLocation
296+
mlirLocationCallSiteGetCallee(MlirLocation location);
297+
298+
/// Getter for caller of CallSite.
299+
MLIR_CAPI_EXPORTED MlirLocation
300+
mlirLocationCallSiteGetCaller(MlirLocation location);
301+
302+
/// TypeID Getter for CallSite.
303+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationCallSiteGetTypeID(void);
304+
305+
/// Checks whether the given location is an CallSite.
306+
MLIR_CAPI_EXPORTED bool mlirLocationIsACallSite(MlirLocation location);
307+
268308
/// Creates a fused location with an array of locations and metadata.
269309
MLIR_CAPI_EXPORTED MlirLocation
270310
mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
271311
MlirLocation const *locations, MlirAttribute metadata);
272312

313+
/// Getter for number of locations fused together.
314+
MLIR_CAPI_EXPORTED unsigned
315+
mlirLocationFusedGetNumLocations(MlirLocation location);
316+
317+
/// Getter for locations of Fused. Requires pre-allocated memory of
318+
/// #fusedLocations X sizeof(MlirLocation).
319+
MLIR_CAPI_EXPORTED void
320+
mlirLocationFusedGetLocations(MlirLocation location,
321+
MlirLocation *locationsCPtr);
322+
323+
/// Getter for metadata of Fused.
324+
MLIR_CAPI_EXPORTED MlirAttribute
325+
mlirLocationFusedGetMetadata(MlirLocation location);
326+
327+
/// TypeID Getter for Fused.
328+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFusedGetTypeID(void);
329+
330+
/// Checks whether the given location is an Fused.
331+
MLIR_CAPI_EXPORTED bool mlirLocationIsAFused(MlirLocation location);
332+
273333
/// Creates a name location owned by the given context. Providing null location
274334
/// for childLoc is allowed and if childLoc is null location, then the behavior
275335
/// is the same as having unknown child location.
276336
MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context,
277337
MlirStringRef name,
278338
MlirLocation childLoc);
279339

340+
/// Getter for name of Name.
341+
MLIR_CAPI_EXPORTED MlirIdentifier
342+
mlirLocationNameGetName(MlirLocation location);
343+
344+
/// Getter for childLoc of Name.
345+
MLIR_CAPI_EXPORTED MlirLocation
346+
mlirLocationNameGetChildLoc(MlirLocation location);
347+
348+
/// TypeID Getter for Name.
349+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationNameGetTypeID(void);
350+
351+
/// Checks whether the given location is an Name.
352+
MLIR_CAPI_EXPORTED bool mlirLocationIsAName(MlirLocation location);
353+
280354
/// Creates a location with unknown position owned by the given context.
281355
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);
282356

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
#include "Globals.h"
1313
#include "IRModule.h"
1414
#include "NanobindUtils.h"
15+
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
1516
#include "mlir-c/BuiltinAttributes.h"
1617
#include "mlir-c/Debug.h"
1718
#include "mlir-c/Diagnostics.h"
1819
#include "mlir-c/IR.h"
1920
#include "mlir-c/Support.h"
2021
#include "mlir/Bindings/Python/Nanobind.h"
2122
#include "mlir/Bindings/Python/NanobindAdaptors.h"
22-
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "llvm/ADT/SmallVector.h"
2525

@@ -299,7 +299,7 @@ struct PyAttrBuilderMap {
299299
return *builder;
300300
}
301301
static void dunderSetItemNamed(const std::string &attributeKind,
302-
nb::callable func, bool replace) {
302+
nb::callable func, bool replace) {
303303
PyGlobals::get().registerAttributeBuilder(attributeKind, std::move(func),
304304
replace);
305305
}
@@ -2933,6 +2933,9 @@ void mlir::python::populateIRCore(nb::module_ &m) {
29332933
nb::arg("callee"), nb::arg("frames"),
29342934
nb::arg("context").none() = nb::none(),
29352935
kContextGetCallSiteLocationDocstring)
2936+
.def("is_a_callsite", mlirLocationIsACallSite)
2937+
.def("get_callee", mlirLocationCallSiteGetCallee)
2938+
.def("get_caller", mlirLocationCallSiteGetCaller)
29362939
.def_static(
29372940
"file",
29382941
[](std::string filename, int line, int col,
@@ -2957,6 +2960,12 @@ void mlir::python::populateIRCore(nb::module_ &m) {
29572960
nb::arg("filename"), nb::arg("start_line"), nb::arg("start_col"),
29582961
nb::arg("end_line"), nb::arg("end_col"),
29592962
nb::arg("context").none() = nb::none(), kContextGetFileRangeDocstring)
2963+
.def("is_a_file", mlirLocationIsAFileLineColRange)
2964+
.def("get_file_name", mlirLocationFileLineColRangeGetFilename)
2965+
.def("get_file_start_line", mlirLocationFileLineColRangeGetStartLine)
2966+
.def("get_file_start_col", mlirLocationFileLineColRangeGetStartColumn)
2967+
.def("get_file_end_line", mlirLocationFileLineColRangeGetStartLine)
2968+
.def("get_file_end_col", mlirLocationFileLineColRangeGetStartColumn)
29602969
.def_static(
29612970
"fused",
29622971
[](const std::vector<PyLocation> &pyLocations,
@@ -2974,6 +2983,14 @@ void mlir::python::populateIRCore(nb::module_ &m) {
29742983
nb::arg("locations"), nb::arg("metadata").none() = nb::none(),
29752984
nb::arg("context").none() = nb::none(),
29762985
kContextGetFusedLocationDocstring)
2986+
.def("is_a_fused", mlirLocationIsAFused)
2987+
.def("get_fused_locations",
2988+
[](MlirLocation loc) {
2989+
unsigned numLocations = mlirLocationFusedGetNumLocations(loc);
2990+
std::vector<MlirLocation> locations(numLocations);
2991+
mlirLocationFusedGetLocations(loc, locations.data());
2992+
return locations;
2993+
})
29772994
.def_static(
29782995
"name",
29792996
[](std::string name, std::optional<PyLocation> childLoc,
@@ -2988,6 +3005,9 @@ void mlir::python::populateIRCore(nb::module_ &m) {
29883005
nb::arg("name"), nb::arg("childLoc").none() = nb::none(),
29893006
nb::arg("context").none() = nb::none(),
29903007
kContextGetNameLocationDocString)
3008+
.def("is_a_name", mlirLocationIsAName)
3009+
.def("get_name_str", mlirLocationNameGetName)
3010+
.def("get_name_child_loc", mlirLocationNameGetChildLoc)
29913011
.def_static(
29923012
"from_attr",
29933013
[](PyAttribute &attribute, DefaultingPyMlirContext context) {

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,54 @@ mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename,
273273
startLine, startCol, endLine, endCol)));
274274
}
275275

276+
MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location) {
277+
return wrap((llvm::cast<FileLineColRange>(unwrap(location)).getFilename()));
278+
}
279+
280+
unsigned mlirLocationFileLineColRangeGetStartLine(MlirLocation location) {
281+
return llvm::cast<FileLineColRange>(unwrap(location)).getStartLine();
282+
}
283+
284+
unsigned mlirLocationFileLineColRangeGetStartColumn(MlirLocation location) {
285+
return llvm::cast<FileLineColRange>(unwrap(location)).getStartColumn();
286+
}
287+
288+
unsigned mlirLocationFileLineColRangeGetEndLine(MlirLocation location) {
289+
return llvm::cast<FileLineColRange>(unwrap(location)).getEndLine();
290+
}
291+
292+
unsigned mlirLocationFileLineColRangeGetEndColumn(MlirLocation location) {
293+
return llvm::cast<FileLineColRange>(unwrap(location)).getEndColumn();
294+
}
295+
296+
MlirTypeID mlirLocationFileLineColRangeGetTypeID() {
297+
return wrap(FileLineColRange::getTypeID());
298+
}
299+
300+
bool mlirLocationIsAFileLineColRange(MlirLocation location) {
301+
return isa<FileLineColRange>(unwrap(location));
302+
}
303+
276304
MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
277305
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
278306
}
279307

308+
MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location) {
309+
return wrap(Location(llvm::cast<CallSiteLoc>(unwrap(location)).getCallee()));
310+
}
311+
312+
MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location) {
313+
return wrap(Location(llvm::cast<CallSiteLoc>(unwrap(location)).getCaller()));
314+
}
315+
316+
MlirTypeID mlirLocationCallSiteGetTypeID() {
317+
return wrap(CallSiteLoc::getTypeID());
318+
}
319+
320+
bool mlirLocationIsACallSite(MlirLocation location) {
321+
return isa<CallSiteLoc>(unwrap(location));
322+
}
323+
280324
MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
281325
MlirLocation const *locations,
282326
MlirAttribute metadata) {
@@ -285,6 +329,30 @@ MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
285329
return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
286330
}
287331

332+
unsigned mlirLocationFusedGetNumLocations(MlirLocation location) {
333+
llvm::ArrayRef<Location> locationsArrRef =
334+
llvm::cast<FusedLoc>(unwrap(location)).getLocations();
335+
return locationsArrRef.size();
336+
}
337+
338+
void mlirLocationFusedGetLocations(MlirLocation location,
339+
MlirLocation *locationsCPtr) {
340+
llvm::ArrayRef<Location> locationsArrRef =
341+
llvm::cast<FusedLoc>(unwrap(location)).getLocations();
342+
for (auto [i, location] : llvm::enumerate(locationsArrRef))
343+
locationsCPtr[i] = wrap(location);
344+
}
345+
346+
MlirAttribute mlirLocationFusedGetMetadata(MlirLocation location) {
347+
return wrap(llvm::cast<FusedLoc>(unwrap(location)).getMetadata());
348+
}
349+
350+
MlirTypeID mlirLocationFusedGetTypeID() { return wrap(FusedLoc::getTypeID()); }
351+
352+
bool mlirLocationIsAFused(MlirLocation location) {
353+
return isa<FusedLoc>(unwrap(location));
354+
}
355+
288356
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
289357
MlirLocation childLoc) {
290358
if (mlirLocationIsNull(childLoc))
@@ -294,6 +362,20 @@ MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
294362
StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
295363
}
296364

365+
MlirIdentifier mlirLocationNameGetName(MlirLocation location) {
366+
return wrap((llvm::cast<NameLoc>(unwrap(location)).getName()));
367+
}
368+
369+
MlirLocation mlirLocationNameGetChildLoc(MlirLocation location) {
370+
return wrap(Location(llvm::cast<NameLoc>(unwrap(location)).getChildLoc()));
371+
}
372+
373+
MlirTypeID mlirLocationNameGetTypeID() { return wrap(NameLoc::getTypeID()); }
374+
375+
bool mlirLocationIsAName(MlirLocation location) {
376+
return isa<NameLoc>(unwrap(location));
377+
}
378+
297379
MlirLocation mlirLocationUnknownGet(MlirContext context) {
298380
return wrap(Location(UnknownLoc::get(unwrap(context))));
299381
}

0 commit comments

Comments
 (0)