Skip to content

[CIR] Upstream StackSave and StackRestoreOp #136426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,50 @@ def CallOp : CIR_CallOpBase<"call", [NoRegionArguments]> {
}]>];
}

//===----------------------------------------------------------------------===//
// StackSaveOp & StackRestoreOp
//===----------------------------------------------------------------------===//

def StackSaveOp : CIR_Op<"stacksave"> {
let summary = "remembers the current state of the function stack";
let description = [{
Saves current state of the function stack. Returns a pointer to an opaque object
that later can be passed into cir.stackrestore.
This is used during the lowering of variable length array allocas.

This operation corresponds to LLVM intrinsic `stacksave`.

```mlir
%0 = cir.stacksave : <!u8i>
```
}];

let results = (outs CIR_PointerType:$result);
let assemblyFormat = "attr-dict `:` qualified(type($result))";
}

def StackRestoreOp : CIR_Op<"stackrestore"> {
let summary = "restores the state of the function stack";
let description = [{
Restore the state of the function stack to the state it was
in when the corresponding cir.stacksave executed.
This is used during the lowering of variable length array allocas.

This operation corresponds to LLVM intrinsic `stackrestore`.

```mlir
%0 = cir.alloca !cir.ptr<!u8i>, !cir.ptr<!cir.ptr<!u8i>>, ["saved_stack"] {alignment = 8 : i64}
%1 = cir.stacksave : <!u8i>
cir.store %1, %0 : !cir.ptr<!u8i>, !cir.ptr<!cir.ptr<!u8i>>
%2 = cir.load %0 : !cir.ptr<!cir.ptr<!u8i>>, !cir.ptr<!u8i>
cir.stackrestore %2 : !cir.ptr<!u8i>
```
}];

let arguments = (ins CIR_PointerType:$ptr);
let assemblyFormat = "$ptr attr-dict `:` qualified(type($ptr))";
}

//===----------------------------------------------------------------------===//
// UnreachableOp
//===----------------------------------------------------------------------===//
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
CIRToLLVMGetGlobalOpLowering,
CIRToLLVMSelectOpLowering,
CIRToLLVMShiftOpLowering,
CIRToLLVMStackSaveOpLowering,
CIRToLLVMStackRestoreOpLowering,
CIRToLLVMTrapOpLowering,
CIRToLLVMUnaryOpLowering
// clang-format on
Expand Down Expand Up @@ -1598,6 +1600,21 @@ mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite(
return mlir::success();
}

mlir::LogicalResult CIRToLLVMStackSaveOpLowering::matchAndRewrite(
cir::StackSaveOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
const mlir::Type ptrTy = getTypeConverter()->convertType(op.getType());
rewriter.replaceOpWithNewOp<mlir::LLVM::StackSaveOp>(op, ptrTy);
return mlir::success();
}

mlir::LogicalResult CIRToLLVMStackRestoreOpLowering::matchAndRewrite(
cir::StackRestoreOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<mlir::LLVM::StackRestoreOp>(op, adaptor.getPtr());
return mlir::success();
}

std::unique_ptr<mlir::Pass> createConvertCIRToLLVMPass() {
return std::make_unique<ConvertCIRToLLVMPass>();
}
Expand Down
21 changes: 21 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ class CIRToLLVMPtrStrideOpLowering
matchAndRewrite(cir::PtrStrideOp op, OpAdaptor,
mlir::ConversionPatternRewriter &) const override;
};

class CIRToLLVMStackSaveOpLowering
: public mlir::OpConversionPattern<cir::StackSaveOp> {
public:
using mlir::OpConversionPattern<cir::StackSaveOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(cir::StackSaveOp op, OpAdaptor,
mlir::ConversionPatternRewriter &) const override;
};

class CIRToLLVMStackRestoreOpLowering
: public mlir::OpConversionPattern<cir::StackRestoreOp> {
public:
using OpConversionPattern<cir::StackRestoreOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(cir::StackRestoreOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override;
};

} // namespace direct
} // namespace cir

Expand Down
23 changes: 23 additions & 0 deletions clang/test/CIR/IR/stack-save-restore.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test the CIR operations can parse and print correctly (roundtrip)

// RUN: cir-opt %s | cir-opt | FileCheck %s

!u8i = !cir.int<u, 8>

module {
cir.func @stack_save_restore() {
%0 = cir.stacksave : !cir.ptr<!u8i>
cir.stackrestore %0 : !cir.ptr<!u8i>
cir.return
}
}

//CHECK: module {

//CHECK-NEXT: cir.func @stack_save_restore() {
//CHECK-NEXT: %0 = cir.stacksave : !cir.ptr<!u8i>
//CHECK-NEXT: cir.stackrestore %0 : !cir.ptr<!u8i>
//CHECK-NEXT: cir.return
//CHECK-NEXT: }

//CHECK-NEXT: }
26 changes: 26 additions & 0 deletions clang/test/CIR/Lowering/stack-save-restore.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a RUN line to lower this to LLVM IR?

// RUN: cir-opt %s -cir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!u8i = !cir.int<u, 8>

module {
cir.func @stack_save() {
%0 = cir.stacksave : !cir.ptr<!u8i>
cir.stackrestore %0 : !cir.ptr<!u8i>
cir.return
}
}

// MLIR: module {
// MLIR-NEXT: llvm.func @stack_save
// MLIR-NEXT: %0 = llvm.intr.stacksave : !llvm.ptr
// MLIR-NEXT: llvm.intr.stackrestore %0 : !llvm.ptr
// MLIR-NEXT: llvm.return
// MLIR-NEXT: }
// MLIR-NEXT: }

// LLVM: define void @stack_save() {
// LLVM: %1 = call ptr @llvm.stacksave.p0()
// LLVM: call void @llvm.stackrestore.p0(ptr %1)
// LLVM: ret void
// LLVM: }
Loading