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 2 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 @@ -1419,6 +1419,50 @@ def CallOp : CIR_CallOpBase<"call", [NoRegionArguments]> {
}]>];
}

//===----------------------------------------------------------------------===//
// StackSave & StackRestoreOp
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// StackSave & StackRestoreOp
// StackSaveOp & StackRestoreOp

For consistency, though I'm not sure why there are combined.

//===----------------------------------------------------------------------===//

def StackSaveOp : CIR_Op<"stack_save"> {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: These should probably mention that operations correspond and mirror semantics of llvm.stacksave and llvm.stackrestore intrinsics.

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Also any reason not to mirror llvm names, i.e. to have cir.stacksave and cir.stackrestore?

Copy link
Member Author

Choose a reason for hiding this comment

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

nit: Also any reason not to mirror llvm names, i.e. to have cir.stacksave and cir.stackrestore?

I found that we use the same name pattern in general, for example get_global, ptr_stride ...etc, but I think in this case we can mirror the llvm names, I will update it

Copy link
Member Author

@AmrDeveloper AmrDeveloper Apr 21, 2025

Choose a reason for hiding this comment

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

Mmmmm, I think that llvm in general uses the same style without _ for example getElemenetPtr, but in CIR dialect we use different style, not sure if it make sense to rename stack_save to stacksave or not, what do you think @xlauko 🤔

Edit: Another example we have is insertvalue and insert_value

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, probably generic question on @bcardosolopes @andykaylor what should be the policy? I don't mind either.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for bringing up the consistency angle here. We have been telling people to use snake_case: https://llvm.github.io/clangir/Dialect/cir-style-guide.html, though I see a good point in matching LLVM when it makes sense (like in this case). I don't have a strong opinion here other than bigger names looks better with _ breaking them down. Maybe we should extend the doc to make things similar to LLVM when is the case?

let summary = "remembers the current state of the function stack";
let description = [{
Remembers the current state of the function stack. Returns a pointer
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Remembers the current state of the function stack. Returns a pointer
Saves current state of the function stack. Returns a pointer to an opaque object

that later can be passed into cir.stack_restore.
Useful for implementing language features like variable length arrays.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Useful for implementing language features like variable length arrays.
This is used during the lowering of variable length array allocas.


This operation is correspond to LLVM intrinsic `stacksave`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
This operation is correspond to LLVM intrinsic `stacksave`.
This operation corresponds to LLVM intrinsic `stacksave`.


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

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

def StackRestoreOp : CIR_Op<"stack_restore"> {
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.stack_save executed.
Useful for implementing language features like variable length arrays.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Useful for implementing language features like variable length arrays.
This is used during the lowering of variable length array allocas.


This operation is correspond to LLVM intrinsic `stackrestore`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
This operation is correspond to LLVM intrinsic `stackrestore`.
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.stack_save : <!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.stack_restore %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 @@ -1467,6 +1467,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
CIRToLLVMConstantOpLowering,
CIRToLLVMFuncOpLowering,
CIRToLLVMGetGlobalOpLowering,
CIRToLLVMStackSaveOpLowering,
CIRToLLVMStackRestoreOpLowering,
CIRToLLVMTrapOpLowering,
CIRToLLVMUnaryOpLowering
// clang-format on
Expand Down Expand Up @@ -1512,6 +1514,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 @@ -242,6 +242,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.stack_save : !cir.ptr<!u8i>
cir.stack_restore %0 : !cir.ptr<!u8i>
cir.return
}
}

//CHECK: module {

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

//CHECK-NEXT: }
19 changes: 19 additions & 0 deletions clang/test/CIR/Lowering/stack-save-restore.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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?


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

module {
cir.func @stack_save() {
%0 = cir.stack_save : !cir.ptr<!u8i>
cir.stack_restore %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: }
Loading