[Mlir-commits] [mlir] [mlir][CAPI][python] bind CallSiteLoc, FileLineColRange, FusedLoc, NameLoc, OpaqueLoc (PR #129351)

Maksim Levental llvmlistbot at llvm.org
Fri Feb 28 19:08:15 PST 2025


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/129351

>From 08545cbb411f78f4f8fb060d47c66037162b5884 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Fri, 28 Feb 2025 22:05:45 -0500
Subject: [PATCH] [mlir][CAPI][python] bind CallSiteLoc, FileLineColRange,
 FusedLoc, NameLoc, OpaqueLoc

---
 mlir/include/mlir-c/BuiltinAttributes.h | 106 +++++++++++++++++++++
 mlir/lib/CAPI/IR/BuiltinAttributes.cpp  | 121 ++++++++++++++++++++++++
 2 files changed, 227 insertions(+)

diff --git a/mlir/include/mlir-c/BuiltinAttributes.h b/mlir/include/mlir-c/BuiltinAttributes.h
index 1d0edf9ea809d..6b499c0623da6 100644
--- a/mlir/include/mlir-c/BuiltinAttributes.h
+++ b/mlir/include/mlir-c/BuiltinAttributes.h
@@ -32,6 +32,112 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeGetNull(void);
 
 MLIR_CAPI_EXPORTED bool mlirAttributeIsALocation(MlirAttribute attr);
 
+/// Attribute builder for CallSiteLoc
+MLIR_CAPI_EXPORTED MlirAttribute mlirCallSiteLocAttrGet(
+    MlirLocation callee, MlirLocation caller, MlirContext mlirContext);
+
+/// Getter for callee of CallSiteLoc
+MLIR_CAPI_EXPORTED MlirLocation
+mlirCallSiteLocGetCallee(MlirAttribute mlirCallSiteLoc);
+
+/// Getter for caller of CallSiteLoc
+MLIR_CAPI_EXPORTED MlirLocation
+mlirCallSiteLocGetCaller(MlirAttribute mlirCallSiteLoc);
+
+/// Attribute builder for FileLineColRange
+MLIR_CAPI_EXPORTED MlirAttribute mlirFileLineColRangeAttrGet(
+    MlirAttribute filename, unsigned startLine, unsigned startColumn,
+    unsigned endLine, unsigned endColumn, MlirContext mlirContext);
+
+/// Getter for filename of FileLineColRange
+MLIR_CAPI_EXPORTED MlirIdentifier
+mlirFileLineColRangeGetFilename(MlirAttribute mlirFileLineColRange);
+
+/// Getter for start_line of FileLineColRange
+MLIR_CAPI_EXPORTED unsigned
+mlirFileLineColRangeGetStartLine(MlirAttribute mlirFileLineColRange);
+
+/// Getter for start_column of FileLineColRange
+MLIR_CAPI_EXPORTED unsigned
+mlirFileLineColRangeGetStartColumn(MlirAttribute mlirFileLineColRange);
+
+/// Getter for end_line of FileLineColRange
+MLIR_CAPI_EXPORTED unsigned
+mlirFileLineColRangeGetEndLine(MlirAttribute mlirFileLineColRange);
+
+/// Getter for end_column of FileLineColRange
+MLIR_CAPI_EXPORTED unsigned
+mlirFileLineColRangeGetEndColumn(MlirAttribute mlirFileLineColRange);
+
+/// Attribute builder for FusedLoc
+MLIR_CAPI_EXPORTED MlirAttribute mlirFusedLocAttrGet(MlirLocation *locations,
+                                                     unsigned nLocations,
+                                                     MlirAttribute metadata,
+                                                     MlirContext mlirContext);
+
+/// Getter for locations of FusedLoc
+MLIR_CAPI_EXPORTED void mlirFusedLocGetLocations(MlirAttribute mlirFusedLoc,
+                                                 MlirLocation **locationsCPtr,
+                                                 unsigned *nLocations);
+
+/// Getter for metadata of FusedLoc
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirFusedLocGetMetadata(MlirAttribute mlirFusedLoc);
+
+/// Attribute builder for NameLoc
+MLIR_CAPI_EXPORTED MlirAttribute mlirNameLocAttrGet(MlirAttribute name,
+                                                    MlirLocation childLoc,
+                                                    MlirContext mlirContext);
+
+/// Getter for name of NameLoc
+MLIR_CAPI_EXPORTED MlirIdentifier mlirNameLocGetName(MlirAttribute mlirNameLoc);
+
+/// Getter for childLoc of NameLoc
+MLIR_CAPI_EXPORTED MlirLocation
+mlirNameLocGetChildLoc(MlirAttribute mlirNameLoc);
+
+/// Attribute builder for OpaqueLoc
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirOpaqueLocAttrGet(uintptr_t underlyingLocation, MlirTypeID underlyingTypeID,
+                     MlirLocation fallbackLocation, MlirContext mlirContext);
+
+/// Getter for underlyingLocation of OpaqueLoc
+MLIR_CAPI_EXPORTED uintptr_t
+mlirOpaqueLocGetUnderlyingLocation(MlirAttribute mlirOpaqueLoc);
+
+/// Getter for underlyingTypeID of OpaqueLoc
+MLIR_CAPI_EXPORTED MlirTypeID
+mlirOpaqueLocGetUnderlyingTypeID(MlirAttribute mlirOpaqueLoc);
+
+/// Getter for fallbackLocation of OpaqueLoc
+MLIR_CAPI_EXPORTED MlirLocation
+mlirOpaqueLocGetFallbackLocation(MlirAttribute mlirOpaqueLoc);
+
+/// TypeID Getter for CallSiteLoc
+MLIR_CAPI_EXPORTED MlirTypeID mlirCallSiteLocGetTypeID(void);
+
+MLIR_CAPI_EXPORTED bool isaMlirCallSiteLoc(MlirAttribute thing);
+
+/// TypeID Getter for FileLineColRange
+MLIR_CAPI_EXPORTED MlirTypeID mlirFileLineColRangeGetTypeID(void);
+
+MLIR_CAPI_EXPORTED bool isaMlirFileLineColRange(MlirAttribute thing);
+
+/// TypeID Getter for FusedLoc
+MLIR_CAPI_EXPORTED MlirTypeID mlirFusedLocGetTypeID(void);
+
+MLIR_CAPI_EXPORTED bool isaMlirFusedLoc(MlirAttribute thing);
+
+/// TypeID Getter for NameLoc
+MLIR_CAPI_EXPORTED MlirTypeID mlirNameLocGetTypeID(void);
+
+MLIR_CAPI_EXPORTED bool isaMlirNameLoc(MlirAttribute thing);
+
+/// TypeID Getter for OpaqueLoc
+MLIR_CAPI_EXPORTED MlirTypeID mlirOpaqueLocGetTypeID(void);
+
+MLIR_CAPI_EXPORTED bool isaMlirOpaqueLoc(MlirAttribute thing);
+
 //===----------------------------------------------------------------------===//
 // Affine map attribute.
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
index 8d57ab6b59e79..a1e8e49ce6310 100644
--- a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
@@ -29,6 +29,127 @@ bool mlirAttributeIsALocation(MlirAttribute attr) {
   return llvm::isa<LocationAttr>(unwrap(attr));
 }
 
+MlirAttribute mlirCallSiteLocAttrGet(MlirLocation callee, MlirLocation caller) {
+  Location callee_ = llvm::cast<Location>(unwrap(callee));
+  Location caller_ = llvm::cast<Location>(unwrap(caller));
+  return wrap(CallSiteLoc::get(callee_, caller_));
+}
+
+MlirLocation mlirCallSiteLocGetCallee(MlirAttribute mlirCallSiteLoc) {
+  return wrap(llvm::cast<CallSiteLoc>(unwrap(mlirCallSiteLoc)).getCallee());
+}
+
+MlirLocation mlirCallSiteLocGetCaller(MlirAttribute mlirCallSiteLoc) {
+  return wrap(llvm::cast<CallSiteLoc>(unwrap(mlirCallSiteLoc)).getCaller());
+}
+
+MlirAttribute mlirFileLineColRangeAttrGet(MlirAttribute filename,
+                                          unsigned startLine,
+                                          unsigned startColumn,
+                                          unsigned endLine, unsigned endColumn,
+                                          MlirContext mlirContext) {
+  mlir::MLIRContext *context = unwrap(mlirContext);
+  StringAttr filename_ = llvm::cast<StringAttr>(unwrap(filename));
+  return wrap(FileLineColRange::get(context, filename_, startLine, startColumn,
+                                    endLine, endColumn));
+}
+
+MlirIdentifier
+mlirFileLineColRangeGetFilename(MlirAttribute mlirFileLineColRange) {
+  return wrap(
+      llvm::cast<FileLineColRange>(unwrap(mlirFileLineColRange)).getFilename());
+}
+
+unsigned mlirFileLineColRangeGetStartLine(MlirAttribute mlirFileLineColRange) {
+  return llvm::cast<FileLineColRange>(unwrap(mlirFileLineColRange))
+      .getStartLine();
+}
+
+unsigned
+mlirFileLineColRangeGetStartColumn(MlirAttribute mlirFileLineColRange) {
+  return llvm::cast<FileLineColRange>(unwrap(mlirFileLineColRange))
+      .getStartColumn();
+}
+
+unsigned mlirFileLineColRangeGetEndLine(MlirAttribute mlirFileLineColRange) {
+  return llvm::cast<FileLineColRange>(unwrap(mlirFileLineColRange))
+      .getEndLine();
+}
+
+unsigned mlirFileLineColRangeGetEndColumn(MlirAttribute mlirFileLineColRange) {
+  return llvm::cast<FileLineColRange>(unwrap(mlirFileLineColRange))
+      .getEndColumn();
+}
+
+MlirAttribute mlirFusedLocAttrGet(MlirLocation const *locations,
+                                  unsigned nLocations, MlirAttribute metadata,
+                                  MlirContext mlirContext) {
+  mlir::MLIRContext *context = unwrap(mlirContext);
+  SmallVector<Location, 8> attrs;
+  llvm::ArrayRef<Location> locations_ =
+      unwrapList(nLocations, locations, attrs);
+  Attribute metadata_ = llvm::cast<Attribute>(unwrap(metadata));
+  return wrap(FusedLoc::get(context, locations_, metadata_));
+}
+
+// void mlirFusedLocGetLocations(MlirAttribute mlirFusedLoc,
+//                               MlirLocation **locationsCPtr,
+//                               unsigned *nLocations) {
+//   llvm::ArrayRef<Location> locations =
+//       llvm::cast<FusedLoc>(unwrap(mlirFusedLoc)).getLocations();
+//   *nLocations = locations.size();
+//   assert(!locations.empty() && "expected non-empty locations");
+//   *locationsCPtr =
+//       static_cast<MlirLocation *>(locations.front().getAsOpaquePointer());
+// }
+
+MlirAttribute mlirFusedLocGetMetadata(MlirAttribute mlirFusedLoc) {
+  return wrap(llvm::cast<FusedLoc>(unwrap(mlirFusedLoc)).getMetadata());
+}
+
+MlirAttribute mlirNameLocAttrGet(MlirAttribute name, MlirLocation childLoc) {
+  StringAttr name_ = llvm::cast<StringAttr>(unwrap(name));
+  Location childLoc_ = llvm::cast<Location>(unwrap(childLoc));
+  return wrap(NameLoc::get(name_, childLoc_));
+}
+
+MlirIdentifier mlirNameLocGetName(MlirAttribute mlirNameLoc) {
+  return wrap(llvm::cast<NameLoc>(unwrap(mlirNameLoc)).getName());
+}
+
+MlirLocation mlirNameLocGetChildLoc(MlirAttribute mlirNameLoc) {
+  return wrap(llvm::cast<NameLoc>(unwrap(mlirNameLoc)).getChildLoc());
+}
+
+MlirAttribute mlirOpaqueLocAttrGet(uintptr_t underlyingLocation,
+                                   MlirTypeID underlyingTypeID,
+                                   MlirLocation fallbackLocation) {
+  TypeID underlyingTypeID_ = llvm::cast<TypeID>(unwrap(underlyingTypeID));
+  Location fallbackLocation_ = llvm::cast<Location>(unwrap(fallbackLocation));
+  return wrap(
+      OpaqueLoc::get(underlyingLocation, underlyingTypeID_, fallbackLocation_));
+}
+
+uintptr_t mlirOpaqueLocGetUnderlyingLocation(MlirAttribute mlirOpaqueLoc) {
+  return llvm::cast<OpaqueLoc>(unwrap(mlirOpaqueLoc)).getUnderlyingLocation();
+}
+
+MlirTypeID mlirOpaqueLocGetUnderlyingTypeID(MlirAttribute mlirOpaqueLoc) {
+  return wrap(
+      llvm::cast<OpaqueLoc>(unwrap(mlirOpaqueLoc)).getUnderlyingTypeID());
+}
+
+MlirLocation mlirOpaqueLocGetFallbackLocation(MlirAttribute mlirOpaqueLoc) {
+  return wrap(
+      llvm::cast<OpaqueLoc>(unwrap(mlirOpaqueLoc)).getFallbackLocation());
+}
+
+MlirTypeID mlirCallSiteLocGetTypeID() { return wrap(CallSiteLoc::getTypeID()); }
+
+bool isaMlirCallSiteLoc(MlirAttribute thing) {
+  return isa<CallSiteLoc>(unwrap(thing));
+}
+
 //===----------------------------------------------------------------------===//
 // Affine map attribute.
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list