https://github.com/aokblast created https://github.com/llvm/llvm-project/pull/230897
We implement ViewLikeOpInterface for dyn_cast, ptr_stride, get_member, get_element, get_runtime_member, base_class_addr, derived_class_addr, and memonic. Although there is no user for CIR internally as decouplePointer returns the base and offset instead of the base directly, it is still useful for external project to do aliasing analysis for MemRef. >From 1beef99a2b642b14f26043e2aa54b18819ac889c Mon Sep 17 00:00:00 2001 From: ShengYi Hung <[email protected]> Date: Sun, 11 Oct 2026 00:32:01 -0500 Subject: [PATCH] [CIR] Implement ViewLikeOpInterface for multiple Ops We implement ViewLikeOpInterface for dyn_cast, ptr_stride, get_member, get_element, get_runtime_member, base_class_addr, derived_class_addr, and memonic. Although there is no user for CIR internally as decouplePointer returns the base and offset instead of the base directly, it is still useful for external project to do aliasing analysis for MemRef. --- .../include/clang/CIR/Dialect/IR/CIRDialect.h | 1 + clang/include/clang/CIR/Dialect/IR/CIROps.td | 30 ++++++++++++++----- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 14 +++++++++ clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 + 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h index d9018e93628b6..b780c07e7751c 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h @@ -25,6 +25,7 @@ #include "mlir/Interfaces/LoopLikeInterface.h" #include "mlir/Interfaces/MemorySlotInterfaces.h" #include "mlir/Interfaces/SideEffectInterfaces.h" +#include "mlir/Interfaces/ViewLikeInterface.h" #include "clang/CIR/Dialect/IR/CIRAttrs.h" #include "clang/CIR/Dialect/IR/CIROpsDialect.h.inc" diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index de805d4c642a2..69ad60482457b 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -32,6 +32,7 @@ include "mlir/Interfaces/InferTypeOpInterface.td" include "mlir/Interfaces/LoopLikeInterface.td" include "mlir/Interfaces/MemorySlotInterfaces.td" include "mlir/Interfaces/SideEffectInterfaces.td" +include "mlir/Interfaces/ViewLikeInterface.td" //===----------------------------------------------------------------------===// // CIR Ops @@ -510,7 +511,8 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> { //===----------------------------------------------------------------------===// def CIR_PtrStrideOp : CIR_Op<"ptr_stride", [ - Pure, AllTypesMatch<["base", "result"]> + Pure, AllTypesMatch<["base", "result"]>, + DeclareOpInterfaceMethods<ViewLikeOpInterface> ]> { let summary = "Pointer access with stride"; let description = [{ @@ -548,7 +550,8 @@ def CIR_PtrStrideOp : CIR_Op<"ptr_stride", [ //===----------------------------------------------------------------------===// def CIR_PtrMaskOp : CIR_Op<"ptr_mask", [ - Pure, AllTypesMatch<["ptr", "result"]> + Pure, AllTypesMatch<["ptr", "result"]>, + DeclareOpInterfaceMethods<ViewLikeOpInterface> ]> { let summary = "Clear bits of a pointer"; let description = [{ @@ -3998,7 +4001,9 @@ def CIR_GetBitfieldOp : CIR_Op<"get_bitfield"> { // GetMemberOp //===----------------------------------------------------------------------===// -def CIR_GetMemberOp : CIR_Op<"get_member", [Pure]> { +def CIR_GetMemberOp : CIR_Op<"get_member", [ + Pure, DeclareOpInterfaceMethods<ViewLikeOpInterface> +]> { let summary = "Get the address of a member of a record"; let description = [{ The `cir.get_member` operation gets the address of a particular named @@ -4161,7 +4166,8 @@ def CIR_GetElementOp : CIR_Op<"get_element", [ "type of 'result' matches element type of 'base'", "base", "result", "cir::PointerType::get(" "mlir::cast<cir::ArrayType>(mlir::cast<cir::PointerType>($_self).getPointee()).getElementType(), " - "mlir::cast<cir::PointerType>($_self).getAddrSpace())"> + "mlir::cast<cir::PointerType>($_self).getAddrSpace())">, + DeclareOpInterfaceMethods<ViewLikeOpInterface> ]> { let summary = "Get the address of an array element"; @@ -5879,7 +5885,9 @@ def CIR_ArrayDtor : CIR_Op<"array.dtor", [ // GetRuntimeMemberOp //===----------------------------------------------------------------------===// -def CIR_GetRuntimeMemberOp : CIR_Op<"get_runtime_member", [Pure]> { +def CIR_GetRuntimeMemberOp : CIR_Op<"get_runtime_member", [ + Pure, DeclareOpInterfaceMethods<ViewLikeOpInterface> +]> { let summary = "Get the address of a member of a record"; let description = [{ The `cir.get_runtime_member` operation gets the address of a member from @@ -6291,7 +6299,9 @@ def CIR_VecSplatOp : CIR_Op<"vec.splat", [ // BaseClassAddrOp //===----------------------------------------------------------------------===// -def CIR_BaseClassAddrOp : CIR_Op<"base_class_addr", [Pure]> { +def CIR_BaseClassAddrOp : CIR_Op<"base_class_addr", [ + Pure, DeclareOpInterfaceMethods<ViewLikeOpInterface> +]> { let summary = "Get the base class address for a class/struct"; let description = [{ The `cir.base_class_addr` operaration gets the address of a particular @@ -6336,7 +6346,9 @@ def CIR_BaseClassAddrOp : CIR_Op<"base_class_addr", [Pure]> { // DerivedClassAddrOp //===----------------------------------------------------------------------===// -def CIR_DerivedClassAddrOp : CIR_Op<"derived_class_addr", [Pure]> { +def CIR_DerivedClassAddrOp : CIR_Op<"derived_class_addr", [ + Pure, DeclareOpInterfaceMethods<ViewLikeOpInterface> +]> { let summary = "Get the derived class address for a class/struct"; let description = [{ The `cir.derived_class_addr` operaration gets the address of a particular @@ -6593,7 +6605,9 @@ def CIR_ComplexImagOp : CIR_ComplexPartOp<"complex.imag"> { // ComplexRealPtrOp and ComplexImagPtrOp //===----------------------------------------------------------------------===// -class CIR_ComplexPartPtrOp<string mnemonic> : CIR_Op<mnemonic, [Pure]> { +class CIR_ComplexPartPtrOp<string mnemonic> : CIR_Op<mnemonic, [ + Pure, DeclareOpInterfaceMethods<ViewLikeOpInterface> +]> { let arguments = (ins CIR_PtrToComplexType:$operand); let results = (outs CIR_PtrToIntOrFloatType:$result); diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index d5a587ff6d81e..235643abf24fc 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -5340,6 +5340,20 @@ bool cir::StdFindOp::signatureMatches(mlir::TypeRange operands, return iterTy == operands[1] && iterTy == operands[2] && iterTy == results[0]; } +//===----------------------------------------------------------------------===// +// ViewLikeOpInterface +//===----------------------------------------------------------------------===// + +mlir::Value cir::PtrStrideOp::getViewSource() { return getBase(); } +mlir::Value cir::PtrMaskOp::getViewSource() { return getPtr(); } +mlir::Value cir::GetMemberOp::getViewSource() { return getAddr(); } +mlir::Value cir::GetElementOp::getViewSource() { return getBase(); } +mlir::Value cir::GetRuntimeMemberOp::getViewSource() { return getAddr(); } +mlir::Value cir::BaseClassAddrOp::getViewSource() { return getDerivedAddr(); } +mlir::Value cir::DerivedClassAddrOp::getViewSource() { return getBaseAddr(); } +mlir::Value cir::ComplexRealPtrOp::getViewSource() { return getOperand(); } +mlir::Value cir::ComplexImagPtrOp::getViewSource() { return getOperand(); } + //===----------------------------------------------------------------------===// // TableGen'd op method definitions //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/IR/CMakeLists.txt b/clang/lib/CIR/Dialect/IR/CMakeLists.txt index 0274005a28858..ac2c35f9fee4d 100644 --- a/clang/lib/CIR/Dialect/IR/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/IR/CMakeLists.txt @@ -21,5 +21,6 @@ add_clang_library(MLIRCIR MLIRFuncDialect MLIRLoopLikeInterface MLIRPtrMemorySpaceInterfaces + MLIRViewLikeInterface clangAST ) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
