Skip to content

Commit 5b37827

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

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,92 @@ 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 locations of Fused.
314+
MLIR_CAPI_EXPORTED void
315+
mlirLocationFusedGetLocations(MlirLocation location,
316+
MlirLocation **locationsCPtr,
317+
unsigned *nLocations);
318+
319+
/// Getter for metadata of Fused.
320+
MLIR_CAPI_EXPORTED MlirAttribute
321+
mlirLocationFusedGetMetadata(MlirLocation location);
322+
323+
/// TypeID Getter for Fused.
324+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFusedGetTypeID(void);
325+
326+
/// Checks whether the given location is an Fused.
327+
MLIR_CAPI_EXPORTED bool mlirLocationisAFused(MlirLocation location);
328+
273329
/// Creates a name location owned by the given context. Providing null location
274330
/// for childLoc is allowed and if childLoc is null location, then the behavior
275331
/// is the same as having unknown child location.
276332
MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context,
277333
MlirStringRef name,
278334
MlirLocation childLoc);
279335

336+
/// Getter for name of Name.
337+
MLIR_CAPI_EXPORTED MlirIdentifier
338+
mlirLocationNameGetName(MlirLocation location);
339+
340+
/// Getter for childLoc of Name.
341+
MLIR_CAPI_EXPORTED MlirLocation
342+
mlirLocationNameGetChildLoc(MlirLocation location);
343+
344+
/// TypeID Getter for Name.
345+
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationNameGetTypeID(void);
346+
347+
/// Checks whether the given location is an Name.
348+
MLIR_CAPI_EXPORTED bool mlirLocationisAName(MlirLocation location);
349+
280350
/// Creates a location with unknown position owned by the given context.
281351
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);
282352

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+
void mlirLocationFusedGetLocations(MlirLocation location,
333+
MlirLocation **locationsCPtr,
334+
unsigned *nLocations) {
335+
llvm::ArrayRef<Location> locationsArrRef =
336+
llvm::cast<FusedLoc>(unwrap(location)).getLocations();
337+
assert(!locationsArrRef.empty() && "expected non-empty locations");
338+
SmallVector<MlirLocation> *locationsVec =
339+
new SmallVector<MlirLocation>(locationsArrRef.size());
340+
for (auto [i, location] : llvm::enumerate(locationsArrRef))
341+
locationsVec->operator[](i) = wrap(location);
342+
*nLocations = locationsVec->size();
343+
*locationsCPtr = locationsVec->data();
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)