Skip to content

[CIR] Fix attributes lowering for GlobalOp #1447

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 1 commit into from
Mar 7, 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
12 changes: 7 additions & 5 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/TypeSwitch.h"
Expand Down Expand Up @@ -2353,10 +2352,10 @@ mlir::LogicalResult CIRToLLVMSwitchFlatOpLowering::matchAndRewrite(
/// insertion point to the end of the initializer block.
void CIRToLLVMGlobalOpLowering::createRegionInitializedLLVMGlobalOp(
cir::GlobalOp op, mlir::Attribute attr,
mlir::ConversionPatternRewriter &rewriter) const {
mlir::ConversionPatternRewriter &rewriter,
SmallVector<mlir::NamedAttribute> attributes) const {
const auto llvmType =
convertTypeForMemory(*getTypeConverter(), dataLayout, op.getSymType());
SmallVector<mlir::NamedAttribute> attributes;
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
op.getSymName(), nullptr,
Expand Down Expand Up @@ -2433,7 +2432,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
// TODO(cir): once LLVM's dialect has proper equivalent attributes this
// should be updated. For now, we use a custom op to initialize globals
// to the appropriate value.
createRegionInitializedLLVMGlobalOp(op, init.value(), rewriter);
createRegionInitializedLLVMGlobalOp(op, init.value(), rewriter,
attributes);
return mlir::success();
} else if (auto constArr =
mlir::dyn_cast<cir::ConstArrayAttr>(init.value())) {
Expand All @@ -2448,7 +2448,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
// Failed to use a compact attribute as an initializer:
// initialize elements individually.
if (!(init = lowerConstArrayAttr(constArr, getTypeConverter()))) {
createRegionInitializedLLVMGlobalOp(op, constArr, rewriter);
createRegionInitializedLLVMGlobalOp(op, constArr, rewriter,
attributes);
return mlir::success();
}
} else {
Expand All @@ -2466,6 +2467,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
auto abiOp = mlir::cast<GlobalOp>(rewriter.clone(*op.getOperation()));
abiOp.setInitialValueAttr(abiValue);
abiOp.setSymType(abiValue.getType());
abiOp->setAttrs(attributes);
rewriter.replaceOp(op, abiOp);
return mlir::success();
} else {
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "llvm/ADT/SmallVector.h"

namespace cir {
namespace direct {
Expand Down Expand Up @@ -622,7 +623,8 @@ class CIRToLLVMGlobalOpLowering
private:
void createRegionInitializedLLVMGlobalOp(
cir::GlobalOp op, mlir::Attribute attr,
mlir::ConversionPatternRewriter &rewriter) const;
mlir::ConversionPatternRewriter &rewriter,
llvm::SmallVector<mlir::NamedAttribute> attributes) const;

mutable mlir::LLVM::ComdatOp comdatOp = nullptr;
static void addComdat(mlir::LLVM::GlobalOp &op,
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/Lowering/attribute-lowering.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM

!s32i = !cir.int<s, 32>

module {
cir.global "private" internal @const_array = #cir.const_array<[#cir.int<1> : !s32i]> : !cir.array<!s32i x 1> {section = ".abc"}
// LLVM: @const_array = internal global [1 x i32] [i32 1], section ".abc"

cir.global "private" internal @const_struct = #cir.const_struct<{#cir.int<1> : !s32i}> : !cir.struct<struct {!s32i}> {section = ".abc"}
// LLVM: @const_struct = internal global { i32 } { i32 1 }, section ".abc"
}