From 5b37827df35407d2695ba45505c611cd7b8027d9 Mon Sep 17 00:00:00 2001 From: Maksim Levental Date: Fri, 28 Feb 2025 22:05:45 -0500 Subject: [PATCH] [mlir][CAPI][python] bind CallSiteLoc, FileLineColRange, FusedLoc, NameLoc, OpaqueLoc --- mlir/include/mlir-c/IR.h | 70 ++++++++++++++++++++++++++++++++++ mlir/lib/CAPI/IR/IR.cpp | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h index 7d2fd89e8560f..87c59dd2905af 100644 --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -261,15 +261,71 @@ 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. @@ -277,6 +333,20 @@ 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); diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp index f27af0ca9a2c7..de1bd19cac82d 100644 --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -273,10 +273,54 @@ mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename, startLine, startCol, endLine, endCol))); } +MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location) { + return wrap((llvm::cast(unwrap(location)).getFilename())); +} + +unsigned mlirLocationFileLineColRangeGetStartLine(MlirLocation location) { + return llvm::cast(unwrap(location)).getStartLine(); +} + +unsigned mlirLocationFileLineColRangeGetStartColumn(MlirLocation location) { + return llvm::cast(unwrap(location)).getStartColumn(); +} + +unsigned mlirLocationFileLineColRangeGetEndLine(MlirLocation location) { + return llvm::cast(unwrap(location)).getEndLine(); +} + +unsigned mlirLocationFileLineColRangeGetEndColumn(MlirLocation location) { + return llvm::cast(unwrap(location)).getEndColumn(); +} + +MlirTypeID mlirLocationFileLineColRangeGetTypeID() { + return wrap(FileLineColRange::getTypeID()); +} + +bool mlirLocationisAFileLineColRange(MlirLocation location) { + return isa(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(unwrap(location)).getCallee())); +} + +MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location) { + return wrap(Location(llvm::cast(unwrap(location)).getCaller())); +} + +MlirTypeID mlirLocationCallSiteGetTypeID() { + return wrap(CallSiteLoc::getTypeID()); +} + +bool mlirLocationisACallSite(MlirLocation location) { + return isa(unwrap(location)); +} + MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, MlirLocation const *locations, MlirAttribute metadata) { @@ -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 locationsArrRef = + llvm::cast(unwrap(location)).getLocations(); + assert(!locationsArrRef.empty() && "expected non-empty locations"); + SmallVector *locationsVec = + new SmallVector(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(unwrap(location)).getMetadata()); +} + +MlirTypeID mlirLocationFusedGetTypeID() { return wrap(FusedLoc::getTypeID()); } + +bool mlirLocationisAFused(MlirLocation location) { + return isa(unwrap(location)); +} + MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, MlirLocation childLoc) { if (mlirLocationIsNull(childLoc)) @@ -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(unwrap(location)).getName())); +} + +MlirLocation mlirLocationNameGetChildLoc(MlirLocation location) { + return wrap(Location(llvm::cast(unwrap(location)).getChildLoc())); +} + +MlirTypeID mlirLocationNameGetTypeID() { return wrap(NameLoc::getTypeID()); } + +bool mlirLocationisAName(MlirLocation location) { + return isa(unwrap(location)); +} + MlirLocation mlirLocationUnknownGet(MlirContext context) { return wrap(Location(UnknownLoc::get(unwrap(context)))); }