Skip to content

Commit 2815b0d

Browse files
committed
[CIR] Fix attributes lowering for GlobalOp
1 parent 7f66a20 commit 2815b0d

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include "llvm/ADT/APInt.h"
5353
#include "llvm/ADT/ArrayRef.h"
5454
#include "llvm/ADT/STLExtras.h"
55-
#include "llvm/ADT/SmallVector.h"
5655
#include "llvm/ADT/StringRef.h"
5756
#include "llvm/ADT/Twine.h"
5857
#include "llvm/ADT/TypeSwitch.h"
@@ -2353,10 +2352,10 @@ mlir::LogicalResult CIRToLLVMSwitchFlatOpLowering::matchAndRewrite(
23532352
/// insertion point to the end of the initializer block.
23542353
void CIRToLLVMGlobalOpLowering::createRegionInitializedLLVMGlobalOp(
23552354
cir::GlobalOp op, mlir::Attribute attr,
2356-
mlir::ConversionPatternRewriter &rewriter) const {
2355+
mlir::ConversionPatternRewriter &rewriter,
2356+
SmallVector<mlir::NamedAttribute> attributes) const {
23572357
const auto llvmType =
23582358
convertTypeForMemory(*getTypeConverter(), dataLayout, op.getSymType());
2359-
SmallVector<mlir::NamedAttribute> attributes;
23602359
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
23612360
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
23622361
op.getSymName(), nullptr,
@@ -2433,7 +2432,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
24332432
// TODO(cir): once LLVM's dialect has proper equivalent attributes this
24342433
// should be updated. For now, we use a custom op to initialize globals
24352434
// to the appropriate value.
2436-
createRegionInitializedLLVMGlobalOp(op, init.value(), rewriter);
2435+
createRegionInitializedLLVMGlobalOp(op, init.value(), rewriter,
2436+
attributes);
24372437
return mlir::success();
24382438
} else if (auto constArr =
24392439
mlir::dyn_cast<cir::ConstArrayAttr>(init.value())) {
@@ -2448,7 +2448,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
24482448
// Failed to use a compact attribute as an initializer:
24492449
// initialize elements individually.
24502450
if (!(init = lowerConstArrayAttr(constArr, getTypeConverter()))) {
2451-
createRegionInitializedLLVMGlobalOp(op, constArr, rewriter);
2451+
createRegionInitializedLLVMGlobalOp(op, constArr, rewriter,
2452+
attributes);
24522453
return mlir::success();
24532454
}
24542455
} else {
@@ -2466,6 +2467,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
24662467
auto abiOp = mlir::cast<GlobalOp>(rewriter.clone(*op.getOperation()));
24672468
abiOp.setInitialValueAttr(abiValue);
24682469
abiOp.setSymType(abiValue.getType());
2470+
abiOp->setAttrs(attributes);
24692471
rewriter.replaceOp(op, abiOp);
24702472
return mlir::success();
24712473
} else {

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1818
#include "mlir/Transforms/DialectConversion.h"
1919
#include "clang/CIR/Dialect/IR/CIRDialect.h"
20+
#include "llvm/ADT/SmallVector.h"
2021

2122
namespace cir {
2223
namespace direct {
@@ -622,7 +623,8 @@ class CIRToLLVMGlobalOpLowering
622623
private:
623624
void createRegionInitializedLLVMGlobalOp(
624625
cir::GlobalOp op, mlir::Attribute attr,
625-
mlir::ConversionPatternRewriter &rewriter) const;
626+
mlir::ConversionPatternRewriter &rewriter,
627+
llvm::SmallVector<mlir::NamedAttribute> attributes) const;
626628

627629
mutable mlir::LLVM::ComdatOp comdatOp = nullptr;
628630
static void addComdat(mlir::LLVM::GlobalOp &op,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM
2+
3+
!s32i = !cir.int<s, 32>
4+
5+
module {
6+
cir.global "private" internal @const_array = #cir.const_array<[#cir.int<1> : !s32i]> : !cir.array<!s32i x 1> {section = ".abc"}
7+
// LLVM: @const_array = internal global [1 x i32] [i32 1], section ".abc"
8+
9+
cir.global "private" internal @const_struct = #cir.const_struct<{#cir.int<1> : !s32i}> : !cir.struct<struct {!s32i}> {section = ".abc"}
10+
// LLVM: @const_struct = internal global { i32} { i32 1 }, section ".abc"
11+
}

0 commit comments

Comments
 (0)