[clang] [CIR] Fix global_view indexing into packed globals (PR #238912)
Arseniy Obolenskiy via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 23:18:32 PST 2026
https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/238912
>From 9c99ce07f44c209a6a8aba4815f918963c941e91 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 4 Nov 2026 19:22:29 +0100
Subject: [PATCH] [CIR] Fix global_view indexing into packed globals
The GEP used the packed struct type introduced in #232835, so the offset was wrong or the GEP failed
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 6 ++-
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 54 ++++++++++++-------
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.h | 7 +++
.../Lowering/const-array-trailing-zeros.cir | 16 ++++++
4 files changed, 61 insertions(+), 22 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index bd762aae3e1d7e..2b8a9f70157854 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -689,7 +689,8 @@ def CIR_ConstantOp : CIR_Op<"const", [
let hasCXXABILowering = true;
let isLLVMLoweringRecursive = true;
let customLLVMLoweringConstructorDecl =
- LoweringBuilders<(ins "LLVMBlockAddressInfo &":$blockInfoAddr)>;
+ LoweringBuilders<(ins "LLVMBlockAddressInfo &":$blockInfoAddr,
+ "GlobalSymbolTypes &":$globalTypes)>;
}
//===----------------------------------------------------------------------===//
@@ -3612,7 +3613,8 @@ def CIR_GlobalOp : CIR_RegionBranchOpBase<"global", [
}];
let customLLVMLoweringConstructorDecl =
- LoweringBuilders<(ins "LLVMBlockAddressInfo &":$blockInfoAddr)>;
+ LoweringBuilders<(ins "LLVMBlockAddressInfo &":$blockInfoAddr,
+ "GlobalSymbolTypes &":$globalTypes)>;
}
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index a48dbd04de84ac..edd3f4d4397bb4 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -497,10 +497,11 @@ class CIRAttrToValue {
mlir::ConversionPatternRewriter &rewriter,
mlir::SymbolTableCollection &symbolTables,
const mlir::TypeConverter *converter,
+ const GlobalSymbolTypes &globalTypes,
LLVMBlockAddressInfo *blockInfoAddr = nullptr)
: parentLoc(parentLoc), parentModule(parentModule), rewriter(rewriter),
symbolTables(symbolTables), converter(converter),
- blockInfoAddr(blockInfoAddr) {}
+ globalTypes(globalTypes), blockInfoAddr(blockInfoAddr) {}
#define GET_CIR_ATTR_TO_VALUE_VISITOR_DECLS
#include "clang/CIR/Dialect/IR/CIRLowering.inc"
@@ -523,6 +524,7 @@ class CIRAttrToValue {
mlir::ConversionPatternRewriter &rewriter;
mlir::SymbolTableCollection &symbolTables;
const mlir::TypeConverter *converter;
+ const GlobalSymbolTypes &globalTypes;
// Only available when lowering global initializers that may contain block
// address attributes. Used to resolve a BlockAddrInfoAttr to its block tag.
LLVMBlockAddressInfo *blockInfoAddr;
@@ -535,9 +537,10 @@ mlir::Value lowerCirAttrAsValue(mlir::Location parentLoc,
mlir::ConversionPatternRewriter &rewriter,
mlir::SymbolTableCollection &symbolTables,
const mlir::TypeConverter *converter,
+ const GlobalSymbolTypes &globalTypes,
LLVMBlockAddressInfo *blockInfoAddr) {
CIRAttrToValue valueConverter(parentLoc, parentModule, rewriter, symbolTables,
- converter, blockInfoAddr);
+ converter, globalTypes, blockInfoAddr);
return valueConverter.visit(attr);
}
@@ -1079,8 +1082,12 @@ CIRAttrToValue::getAddressOfSymbol(mlir::FlatSymbolRefAttr symbol) {
mlir::Operation *sourceSymbol =
symbolTables.lookupSymbolIn(parentModule, symbol);
if (auto llvmSymbol = dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
- sourceType = llvmSymbol.getType();
symName = llvmSymbol.getSymName();
+ // The initializer may have reshaped the global, so use the type recorded
+ // before that. Globals not lowered from cir.global have none.
+ sourceType = globalTypes.lookup(symName);
+ if (!sourceType)
+ sourceType = llvmSymbol.getType();
addrSpace = llvmSymbol.getAddrSpace();
} else if (auto cirSymbol = dyn_cast<cir::GlobalOp>(sourceSymbol)) {
originalType = cirSymbol.getSymType();
@@ -2851,9 +2858,9 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
// Lower GlobalViewAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint and
// BlockAddrDiffAttr to the difference of two llvm.blockaddress.
if (mlir::isa<cir::GlobalViewAttr, cir::BlockAddrDiffAttr>(op.getValue())) {
- auto newOp =
- lowerCirAttrAsValue(op->getLoc(), moduleOp, op.getValue(), rewriter,
- symbolTables, getTypeConverter(), &blockInfoAddr);
+ auto newOp = lowerCirAttrAsValue(
+ op->getLoc(), moduleOp, op.getValue(), rewriter, symbolTables,
+ getTypeConverter(), globalTypes, &blockInfoAddr);
rewriter.replaceOp(op, newOp);
return mlir::success();
}
@@ -2879,9 +2886,9 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
// llvm.blockaddress.
if (mlir::isa<cir::GlobalViewAttr, cir::GlobalOffsetAttr, cir::ConstPtrAttr,
cir::BlockAddrInfoAttr>(op.getValue())) {
- auto newOp =
- lowerCirAttrAsValue(op->getLoc(), moduleOp, op.getValue(), rewriter,
- symbolTables, getTypeConverter(), &blockInfoAddr);
+ auto newOp = lowerCirAttrAsValue(
+ op->getLoc(), moduleOp, op.getValue(), rewriter, symbolTables,
+ getTypeConverter(), globalTypes, &blockInfoAddr);
if (!newOp)
return mlir::failure();
rewriter.replaceOp(op, newOp);
@@ -2900,8 +2907,9 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
typeConverter))) {
attr = denseAttr.value();
} else {
- const mlir::Value initVal = lowerCirAttrAsValue(
- op->getLoc(), moduleOp, value, rewriter, symbolTables, typeConverter);
+ const mlir::Value initVal =
+ lowerCirAttrAsValue(op->getLoc(), moduleOp, value, rewriter,
+ symbolTables, typeConverter, globalTypes);
if (!initVal)
return mlir::failure();
rewriter.replaceOp(op, initVal);
@@ -2909,9 +2917,9 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
}
} else if (const auto recordAttr =
mlir::dyn_cast<cir::ConstRecordAttr>(op.getValue())) {
- auto initVal = lowerCirAttrAsValue(op->getLoc(), moduleOp,
- spellOutSplitTrailingZeros(recordAttr),
- rewriter, symbolTables, typeConverter);
+ auto initVal = lowerCirAttrAsValue(
+ op->getLoc(), moduleOp, spellOutSplitTrailingZeros(recordAttr),
+ rewriter, symbolTables, typeConverter, globalTypes);
if (!initVal)
return mlir::failure();
rewriter.replaceOp(op, initVal);
@@ -2919,15 +2927,16 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
} else if (const auto vecTy = mlir::dyn_cast<cir::VectorType>(op.getType())) {
mlir::Value initVal =
lowerCirAttrAsValue(op->getLoc(), moduleOp, op.getValue(), rewriter,
- symbolTables, getTypeConverter());
+ symbolTables, getTypeConverter(), globalTypes);
if (!initVal)
return mlir::failure();
rewriter.replaceOp(op, initVal);
return mlir::success();
} else if (mlir::isa<cir::RecordType>(op.getType())) {
if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(attr)) {
- mlir::Value initVal = lowerCirAttrAsValue(
- op->getLoc(), moduleOp, attr, rewriter, symbolTables, typeConverter);
+ mlir::Value initVal =
+ lowerCirAttrAsValue(op->getLoc(), moduleOp, attr, rewriter,
+ symbolTables, typeConverter, globalTypes);
if (!initVal)
return mlir::failure();
rewriter.replaceOp(op, initVal);
@@ -3427,7 +3436,7 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal(
// initializer or nested inside an aggregate) can be resolved by the
// BlockAddrInfoAttr visitor.
CIRAttrToValue valueConverter(loc, moduleOp, rewriter, symbolTables,
- typeConverter, &blockInfoAddr);
+ typeConverter, globalTypes, &blockInfoAddr);
mlir::Value value = valueConverter.visit(init);
if (!value)
return mlir::failure();
@@ -3459,6 +3468,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
if (!llvmType)
return op.emitError()
<< "NYI: lowering global of a type with no memory representation";
+ globalTypes[op.getSymName()] = llvmType;
// A flexible array member initializer makes the constant larger than the
// record's declared type, so the global must use an oversized anonymous
@@ -4891,10 +4901,14 @@ void ConvertCIRToLLVMPass::runOnOperation() {
/// Cached symbol table collection used by call lowering patterns to avoid
/// repeated O(M) module-wide symbol scans for every call site.
mlir::SymbolTableCollection symbolTables;
+ /// Memory type of each cir.global before its initializer reshapes it.
+ GlobalSymbolTypes globalTypes;
mlir::RewritePatternSet patterns(&getContext());
- patterns.add<CIRToLLVMBlockAddressOpLowering, CIRToLLVMConstantOpLowering,
- CIRToLLVMGlobalOpLowering, CIRToLLVMLabelOpLowering>(
+ patterns.add<CIRToLLVMBlockAddressOpLowering, CIRToLLVMLabelOpLowering>(
converter, patterns.getContext(), dl, symbolTables, blockInfoAddr);
+ patterns.add<CIRToLLVMConstantOpLowering, CIRToLLVMGlobalOpLowering>(
+ converter, patterns.getContext(), dl, symbolTables, blockInfoAddr,
+ globalTypes);
patterns.add<
#define GET_LLVM_LOWERING_PATTERNS_LIST
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
index 969c16de651c87..5151da94a81aa4 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
@@ -17,6 +17,7 @@
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
+#include "llvm/ADT/StringMap.h"
namespace cir {
@@ -24,6 +25,11 @@ namespace direct {
struct LLVMBlockAddressInfo;
+/// Memory type of each lowered cir.global, keyed by symbol name. It is the type
+/// that global_view indices apply to, which differs from the type of the
+/// llvm.mlir.global when its initializer reshapes it.
+using GlobalSymbolTypes = llvm::StringMap<mlir::Type>;
+
/// Convert a CIR attribute to an LLVM value, or return null on failure.
/// May use the datalayout for lowering attributes to-be-stored in memory. When
/// the attribute may contain block address attributes, `blockInfoAddr` is used
@@ -34,6 +40,7 @@ mlir::Value lowerCirAttrAsValue(mlir::Location parentLoc,
mlir::ConversionPatternRewriter &rewriter,
mlir::SymbolTableCollection &symbolTables,
const mlir::TypeConverter *converter,
+ const GlobalSymbolTypes &globalTypes,
LLVMBlockAddressInfo *blockInfoAddr = nullptr);
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
diff --git a/clang/test/CIR/Lowering/const-array-trailing-zeros.cir b/clang/test/CIR/Lowering/const-array-trailing-zeros.cir
index 3c34831700e999..5343ddd0eb8a79 100644
--- a/clang/test/CIR/Lowering/const-array-trailing-zeros.cir
+++ b/clang/test/CIR/Lowering/const-array-trailing-zeros.cir
@@ -66,6 +66,22 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
cir.global external @q_large = #cir.const_array<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i, #cir.int<9> : !s32i, #cir.int<10> : !s32i, #cir.int<11> : !s32i, #cir.int<12> : !s32i, #cir.int<13> : !s32i, #cir.int<14> : !s32i, #cir.int<15> : !s32i, #cir.int<16> : !s32i, #cir.int<17> : !s32i, #cir.int<18> : !s32i, #cir.int<19> : !s32i, #cir.int<20> : !s32i, #cir.int<21> : !s32i, #cir.int<22> : !s32i, #cir.int<23> : !s32i, #cir.int<24> : !s32i, #cir.int<25> : !s32i, #cir.int<26> : !s32i, #cir.int<27> : !s32i, #cir.int<28> : !s32i, #cir.int<29> : !s32i, #cir.int<30> : !s32i, #cir.int<31> : !s32i, #cir.int<32> : !s32i, #cir.int<33> : !s32i, #cir.int<34> : !s32i, #cir.int<35> : !s32i, #cir.int<36> : !s32i, #cir.int<37> : !s32i, #cir.int<38> : !s32i, #cir.int<39> : !s32i, #cir.int<40> : !s32i, #cir.int<41> : !s32i, #cir.int<42> : !s32i, #cir.int<43> : !s32i, #cir.int<44> : !s32i, #cir.int<45> : !s32i, #cir.int<46> : !s32i, #cir.int<47> : !s32i, #cir.int<48> : !s32i, #cir.int<49> : !s32i, #cir.int<50> : !s32i, #cir.int<51> : !s32i, #cir.int<52> : !s32i, #cir.int<53> : !s32i, #cir.int<54> : !s32i, #cir.int<55> : !s32i, #cir.int<56> : !s32i, #cir.int<57> : !s32i, #cir.int<58> : !s32i, #cir.int<59> : !s32i, #cir.int<60> : !s32i, #cir.int<61> : !s32i, #cir.int<62> : !s32i, #cir.int<63> : !s32i, #cir.int<64> : !s32i, #cir.int<65> : !s32i, #cir.int<66> : !s32i, #cir.int<67> : !s32i, #cir.int<68> : !s32i, #cir.int<69> : !s32i, #cir.int<70> : !s32i, #cir.int<71> : !s32i, #cir.int<72> : !s32i, #cir.int<73> : !s32i, #cir.int<74> : !s32i, #cir.int<75> : !s32i, #cir.int<76> : !s32i, #cir.int<77> : !s32i, #cir.int<78> : !s32i, #cir.int<79> : !s32i, #cir.int<80> : !s32i, #cir.int<81> : !s32i, #cir.int<82> : !s32i, #cir.int<83> : !s32i, #cir.int<84> : !s32i, #cir.int<85> : !s32i, #cir.int<86> : !s32i, #cir.int<87> : !s32i, #cir.int<88> : !s32i, #cir.int<89> : !s32i, #cir.int<90> : !s32i, #cir.int<91> : !s32i, #cir.int<92> : !s32i, #cir.int<93> : !s32i, #cir.int<94> : !s32i, #cir.int<95> : !s32i, #cir.int<96> : !s32i, #cir.int<97> : !s32i, #cir.int<98> : !s32i, #cir.int<99> : !s32i, #cir.int<100> : !s32i], trailing_zeros> : !cir.array<!s32i x 108>
// CHECK: llvm.mlir.global external @q_large([dense<[1, 2, 3, {{.*}}, 99, 100]> : tensor<100xi32>, #llvm.zero]) {{.*}} : !llvm.struct<packed (array<100 x i32>, array<8 x i32>)>
+ // A global_view indexes the type of the global, not the packed struct it
+ // lowers to.
+ cir.global external @view = #cir.global_view<@g, [5 : i32]> : !cir.ptr<!s32i>
+ // CHECK-LABEL: llvm.mlir.global external @view()
+ // CHECK: %[[G:.*]] = llvm.mlir.addressof @g : !llvm.ptr
+ // CHECK: llvm.getelementptr %[[G]][0, 5] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<10 x i32>
+
+ cir.func @view_const(%arg0: !cir.ptr<!cir.ptr<!s32i>>) {
+ %0 = cir.const #cir.global_view<@g, [5 : i32]> : !cir.ptr<!s32i>
+ cir.store %0, %arg0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+ cir.return
+ }
+ // CHECK-LABEL: llvm.func @view_const
+ // CHECK: %[[G:.*]] = llvm.mlir.addressof @g : !llvm.ptr
+ // CHECK: llvm.getelementptr %[[G]][0, 5] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<10 x i32>
+
cir.func @array(%arg0: !cir.ptr<!cir.array<!s32i x 10>>) {
%0 = cir.const #cir.const_array<[#cir.int<1> : !s32i], trailing_zeros> : !cir.array<!s32i x 10>
cir.store %0, %arg0 : !cir.array<!s32i x 10>, !cir.ptr<!cir.array<!s32i x 10>>
More information about the cfe-commits
mailing list