Skip to content

Commit

Permalink
[mlir][CAPI][python] bind CallSiteLoc, FileLineColRange, FusedLoc, Na…
Browse files Browse the repository at this point in the history
…meLoc, OpaqueLoc
  • Loading branch information
makslevental committed Mar 3, 2025
1 parent 7612dcc commit 5b37827
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
70 changes: 70 additions & 0 deletions mlir/include/mlir-c/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,92 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(
MlirContext context, MlirStringRef filename, unsigned start_line,
unsigned start_col, unsigned end_line, unsigned end_col);

/// Getter for filename of FileLineColRange.
MLIR_CAPI_EXPORTED MlirIdentifier
mlirLocationFileLineColRangeGetFilename(MlirLocation location);

/// Getter for start_line of FileLineColRange.
MLIR_CAPI_EXPORTED unsigned
mlirLocationFileLineColRangeGetStartLine(MlirLocation location);

/// Getter for start_column of FileLineColRange.
MLIR_CAPI_EXPORTED unsigned
mlirLocationFileLineColRangeGetStartColumn(MlirLocation location);

/// Getter for end_line of FileLineColRange.
MLIR_CAPI_EXPORTED unsigned
mlirLocationFileLineColRangeGetEndLine(MlirLocation location);

/// Getter for end_column of FileLineColRange.
MLIR_CAPI_EXPORTED unsigned
mlirLocationFileLineColRangeGetEndColumn(MlirLocation location);

/// TypeID Getter for FileLineColRange.
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFileLineColRangeGetTypeID(void);

/// Checks whether the given location is an FileLineColRange.
MLIR_CAPI_EXPORTED bool mlirLocationisAFileLineColRange(MlirLocation location);

/// Creates a call site location with a callee and a caller.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
MlirLocation caller);

/// Getter for callee of CallSite.
MLIR_CAPI_EXPORTED MlirLocation
mlirLocationCallSiteGetCallee(MlirLocation location);

/// Getter for caller of CallSite.
MLIR_CAPI_EXPORTED MlirLocation
mlirLocationCallSiteGetCaller(MlirLocation location);

/// TypeID Getter for CallSite.
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationCallSiteGetTypeID(void);

/// Checks whether the given location is an CallSite.
MLIR_CAPI_EXPORTED bool mlirLocationisACallSite(MlirLocation location);

/// Creates a fused location with an array of locations and metadata.
MLIR_CAPI_EXPORTED MlirLocation
mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
MlirLocation const *locations, MlirAttribute metadata);

/// Getter for locations of Fused.
MLIR_CAPI_EXPORTED void
mlirLocationFusedGetLocations(MlirLocation location,
MlirLocation **locationsCPtr,
unsigned *nLocations);

/// Getter for metadata of Fused.
MLIR_CAPI_EXPORTED MlirAttribute
mlirLocationFusedGetMetadata(MlirLocation location);

/// TypeID Getter for Fused.
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFusedGetTypeID(void);

/// Checks whether the given location is an Fused.
MLIR_CAPI_EXPORTED bool mlirLocationisAFused(MlirLocation location);

/// 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);

/// Getter for name of Name.
MLIR_CAPI_EXPORTED MlirIdentifier
mlirLocationNameGetName(MlirLocation location);

/// Getter for childLoc of Name.
MLIR_CAPI_EXPORTED MlirLocation
mlirLocationNameGetChildLoc(MlirLocation location);

/// TypeID Getter for Name.
MLIR_CAPI_EXPORTED MlirTypeID mlirLocationNameGetTypeID(void);

/// Checks whether the given location is an Name.
MLIR_CAPI_EXPORTED bool mlirLocationisAName(MlirLocation location);

/// Creates a location with unknown position owned by the given context.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);

Expand Down
82 changes: 82 additions & 0 deletions mlir/lib/CAPI/IR/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,54 @@ mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename,
startLine, startCol, endLine, endCol)));
}

MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location) {
return wrap((llvm::cast<FileLineColRange>(unwrap(location)).getFilename()));
}

unsigned mlirLocationFileLineColRangeGetStartLine(MlirLocation location) {
return llvm::cast<FileLineColRange>(unwrap(location)).getStartLine();
}

unsigned mlirLocationFileLineColRangeGetStartColumn(MlirLocation location) {
return llvm::cast<FileLineColRange>(unwrap(location)).getStartColumn();
}

unsigned mlirLocationFileLineColRangeGetEndLine(MlirLocation location) {
return llvm::cast<FileLineColRange>(unwrap(location)).getEndLine();
}

unsigned mlirLocationFileLineColRangeGetEndColumn(MlirLocation location) {
return llvm::cast<FileLineColRange>(unwrap(location)).getEndColumn();
}

MlirTypeID mlirLocationFileLineColRangeGetTypeID() {
return wrap(FileLineColRange::getTypeID());
}

bool mlirLocationisAFileLineColRange(MlirLocation location) {
return isa<FileLineColRange>(unwrap(location));
}

MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
}

MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location) {
return wrap(Location(llvm::cast<CallSiteLoc>(unwrap(location)).getCallee()));
}

MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location) {
return wrap(Location(llvm::cast<CallSiteLoc>(unwrap(location)).getCaller()));
}

MlirTypeID mlirLocationCallSiteGetTypeID() {
return wrap(CallSiteLoc::getTypeID());
}

bool mlirLocationisACallSite(MlirLocation location) {
return isa<CallSiteLoc>(unwrap(location));
}

MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
MlirLocation const *locations,
MlirAttribute metadata) {
Expand All @@ -285,6 +329,30 @@ MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
}

void mlirLocationFusedGetLocations(MlirLocation location,
MlirLocation **locationsCPtr,
unsigned *nLocations) {
llvm::ArrayRef<Location> locationsArrRef =
llvm::cast<FusedLoc>(unwrap(location)).getLocations();
assert(!locationsArrRef.empty() && "expected non-empty locations");
SmallVector<MlirLocation> *locationsVec =
new SmallVector<MlirLocation>(locationsArrRef.size());
for (auto [i, location] : llvm::enumerate(locationsArrRef))
locationsVec->operator[](i) = wrap(location);
*nLocations = locationsVec->size();
*locationsCPtr = locationsVec->data();
}

MlirAttribute mlirLocationFusedGetMetadata(MlirLocation location) {
return wrap(llvm::cast<FusedLoc>(unwrap(location)).getMetadata());
}

MlirTypeID mlirLocationFusedGetTypeID() { return wrap(FusedLoc::getTypeID()); }

bool mlirLocationisAFused(MlirLocation location) {
return isa<FusedLoc>(unwrap(location));
}

MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
MlirLocation childLoc) {
if (mlirLocationIsNull(childLoc))
Expand All @@ -294,6 +362,20 @@ MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
}

MlirIdentifier mlirLocationNameGetName(MlirLocation location) {
return wrap((llvm::cast<NameLoc>(unwrap(location)).getName()));
}

MlirLocation mlirLocationNameGetChildLoc(MlirLocation location) {
return wrap(Location(llvm::cast<NameLoc>(unwrap(location)).getChildLoc()));
}

MlirTypeID mlirLocationNameGetTypeID() { return wrap(NameLoc::getTypeID()); }

bool mlirLocationisAName(MlirLocation location) {
return isa<NameLoc>(unwrap(location));
}

MlirLocation mlirLocationUnknownGet(MlirContext context) {
return wrap(Location(UnknownLoc::get(unwrap(context))));
}
Expand Down

0 comments on commit 5b37827

Please sign in to comment.