diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index bf317bcd07ec..55d5396cc84e 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -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" @@ -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 attributes) const { const auto llvmType = convertTypeForMemory(*getTypeConverter(), dataLayout, op.getSymType()); - SmallVector attributes; auto newGlobalOp = rewriter.replaceOpWithNewOp( op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()), op.getSymName(), nullptr, @@ -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(init.value())) { @@ -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 { @@ -2466,6 +2467,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite( auto abiOp = mlir::cast(rewriter.clone(*op.getOperation())); abiOp.setInitialValueAttr(abiValue); abiOp.setSymType(abiValue.getType()); + abiOp->setAttrs(attributes); rewriter.replaceOp(op, abiOp); return mlir::success(); } else { diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h index fe625afe2918..24ab54896133 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h @@ -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 { @@ -622,7 +623,8 @@ class CIRToLLVMGlobalOpLowering private: void createRegionInitializedLLVMGlobalOp( cir::GlobalOp op, mlir::Attribute attr, - mlir::ConversionPatternRewriter &rewriter) const; + mlir::ConversionPatternRewriter &rewriter, + llvm::SmallVector attributes) const; mutable mlir::LLVM::ComdatOp comdatOp = nullptr; static void addComdat(mlir::LLVM::GlobalOp &op, diff --git a/clang/test/CIR/Lowering/attribute-lowering.cir b/clang/test/CIR/Lowering/attribute-lowering.cir new file mode 100644 index 000000000000..c5436fe04ff0 --- /dev/null +++ b/clang/test/CIR/Lowering/attribute-lowering.cir @@ -0,0 +1,11 @@ +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM + +!s32i = !cir.int + +module { + cir.global "private" internal @const_array = #cir.const_array<[#cir.int<1> : !s32i]> : !cir.array {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 {section = ".abc"} + // LLVM: @const_struct = internal global { i32 } { i32 1 }, section ".abc" +}