From d7d7b73a276a54d87efc70ac748f1abc79d5cf48 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 12:31:33 +0300 Subject: [PATCH 01/31] kind of works, but dirty and tests fail --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 61 ++++++++++++++----- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 8 ++- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index c93a145f35ce..d35c6b64671a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -9,7 +9,7 @@ // This is the internal per-translation-unit state used for CIR translation. // //===----------------------------------------------------------------------===// - +#include #include "CIRGenModule.h" #include "CIRGenCXXABI.h" @@ -799,7 +799,6 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { assert(Old.getSymName() == New.getSymName() && "symbol names must match"); - // If the types does not match, update all references to Old to the new type. auto OldTy = Old.getSymType(); auto NewTy = New.getSymType(); @@ -809,7 +808,22 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { if (oldAS != newAS) { llvm_unreachable("NYI"); } + if (OldTy != NewTy) { + + auto changeType = [&](GetGlobalOp op) { + auto UseOpResultValue = op.getAddr(); + UseOpResultValue.setType( + cir::PointerType::get(&getMLIRContext(), NewTy)); + + mlir::OpBuilder::InsertionGuard guard(builder); + builder.setInsertionPointAfter(op); + mlir::Type ptrTy = builder.getPointerTo(OldTy); + mlir::Value cast = + builder.createBitcast(op->getLoc(), UseOpResultValue, ptrTy); + UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()}); + }; + auto OldSymUses = Old.getSymbolUses(theModule.getOperation()); if (OldSymUses.has_value()) { for (auto Use : *OldSymUses) { @@ -818,17 +832,36 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { "GlobalOp symbol user is neither a GetGlobalOp nor a GlobalOp"); if (auto GGO = dyn_cast(Use.getUser())) { - auto UseOpResultValue = GGO.getAddr(); - UseOpResultValue.setType( - cir::PointerType::get(&getMLIRContext(), NewTy)); - - mlir::OpBuilder::InsertionGuard guard(builder); - builder.setInsertionPointAfter(UserOp); - mlir::Type ptrTy = builder.getPointerTo(OldTy); - mlir::Value cast = - builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy); - UseOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp()); - } + changeType(GGO); + } else if (auto glob = dyn_cast(UserOp)) { + if (auto init = glob.getInitialValue()) { + if (auto attr = mlir::dyn_cast(init.value())) { + if (auto ari = attr.getIndices()) { + auto oldSTy = dyn_cast(OldTy); + if (oldSTy && ari.size() == 1) { + auto intAttr = dyn_cast(ari[0]); + auto oldInd = intAttr.getValue().getSExtValue(); + auto oldSTy = cast(OldTy); + auto& layout = getDataLayout(); + + llvm::SmallVector newIndx; + auto offset = oldSTy.getElementOffset(layout.layout, oldInd); + builder.computeGlobalViewIndicesFromFlatOffset(offset, NewTy, layout, newIndx); + + llvm::SmallVector attrs; + for (auto ind : newIndx) { + auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(builder.getContext(), 32), ind); + attrs.push_back(a); + } + auto arAttr = mlir::ArrayAttr::get(builder.getContext(), attrs); + auto typ = cir::PointerType::get(builder.getContext(), UCharTy); + auto newView = builder.getGlobalViewAttr(typ, New, arAttr); + glob.setInitialValueAttr(newView); + } + } + } + } + } } } } @@ -1082,7 +1115,7 @@ CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *D, mlir::Type Ty, if (!Ty) Ty = getTypes().convertTypeForMem(ASTTy); - auto globalOp = getOrCreateCIRGlobal(D, Ty, IsForDefinition); + auto globalOp = getOrCreateCIRGlobal(D, Ty, IsForDefinition); return builder.getGlobalViewAttr(builder.getPointerTo(Ty), globalOp); } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 3462e90ff5f4..f27d7bff6c4e 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -9,6 +9,9 @@ // This file implements lowering of CIR operations to LLVMIR. // //===----------------------------------------------------------------------===// + +#include + #include "LowerToLLVM.h" #include "LoweringHelpers.h" #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" @@ -650,7 +653,8 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, auto *sourceSymbol = mlir::SymbolTable::lookupSymbolIn(module, globalAttr.getSymbol()); if (auto llvmSymbol = dyn_cast(sourceSymbol)) { - sourceType = llvmSymbol.getType(); + auto typ = dyn_cast(globalAttr.getType()).getPointee(); + sourceType = converter->convertType(typ); symName = llvmSymbol.getSymName(); sourceAddrSpace = llvmSymbol.getAddrSpace(); } else if (auto cirSymbol = dyn_cast(sourceSymbol)) { @@ -695,7 +699,7 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, auto resTy = addrOp.getType(); auto eltTy = converter->convertType(sourceType); addrOp = rewriter.create(loc, resTy, eltTy, addrOp, - indices, true); + indices, true); } if (auto intTy = mlir::dyn_cast(globalAttr.getType())) { From b4a3f15f5bc01b00d9e007ffc8385e68f1e102a4 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 13:11:45 +0300 Subject: [PATCH 02/31] wip --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index b886674b366f..47ccc34cdaaf 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -9,6 +9,8 @@ // This contains code to emit Constant Expr nodes as LLVM code. // //===----------------------------------------------------------------------===// +#include + #include "Address.h" #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" @@ -159,7 +161,7 @@ static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { } bool ConstantAggregateBuilder::add(mlir::Attribute A, CharUnits Offset, - bool AllowOverwrite) { + bool AllowOverwrite) { // FIXME(cir): migrate most of this file to use mlir::TypedAttr directly. mlir::TypedAttr C = mlir::dyn_cast(A); assert(C && "expected typed attribute"); @@ -385,7 +387,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom( CharUnits AlignedSize = Size.alignTo(Align); bool Packed = false; - ArrayRef UnpackedElems = Elems; + ArrayRef UnpackedElems = Elems; llvm::SmallVector UnpackedElemStorage; if (DesiredSize < AlignedSize || DesiredSize.alignTo(Align) != DesiredSize) { @@ -396,7 +398,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom( // is ignored if we choose a packed layout.) UnpackedElemStorage.assign(UnpackedElems.begin(), UnpackedElems.end()); UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size)); - UnpackedElems = UnpackedElemStorage; + UnpackedElems = UnpackedElemStorage; } // If we don't have a natural layout, insert padding as necessary. @@ -648,8 +650,15 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { Expr *Init = nullptr; if (ElementNo < ILE->getNumInits()) Init = ILE->getInit(ElementNo++); - if (Init && isa(Init)) + + if (Init && isa(Init)) { + if (ZeroInitPadding && + !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, + ZeroFieldSize)) + return false; + continue; + } // Zero-sized fields are not emitted, but their initializers may still // prevent emission of this struct as a constant. @@ -658,6 +667,11 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { return false; continue; } + + if (ZeroInitPadding && + !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, + ZeroFieldSize)) + return false; if (ZeroInitPadding && !ApplyZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, From 02525853b3eb8884b9f329dc5be51db78f13a7b8 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 14:22:33 +0300 Subject: [PATCH 03/31] [CIR][CodeGen] handle zero init padding case --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index 47ccc34cdaaf..8b7e0358a378 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -9,8 +9,6 @@ // This contains code to emit Constant Expr nodes as LLVM code. // //===----------------------------------------------------------------------===// -#include - #include "Address.h" #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" @@ -161,7 +159,7 @@ static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { } bool ConstantAggregateBuilder::add(mlir::Attribute A, CharUnits Offset, - bool AllowOverwrite) { + bool AllowOverwrite) { // FIXME(cir): migrate most of this file to use mlir::TypedAttr directly. mlir::TypedAttr C = mlir::dyn_cast(A); assert(C && "expected typed attribute"); @@ -387,7 +385,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom( CharUnits AlignedSize = Size.alignTo(Align); bool Packed = false; - ArrayRef UnpackedElems = Elems; + ArrayRef UnpackedElems = Elems; llvm::SmallVector UnpackedElemStorage; if (DesiredSize < AlignedSize || DesiredSize.alignTo(Align) != DesiredSize) { @@ -398,7 +396,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom( // is ignored if we choose a packed layout.) UnpackedElemStorage.assign(UnpackedElems.begin(), UnpackedElems.end()); UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size)); - UnpackedElems = UnpackedElemStorage; + UnpackedElems = UnpackedElemStorage; } // If we don't have a natural layout, insert padding as necessary. @@ -650,15 +648,8 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { Expr *Init = nullptr; if (ElementNo < ILE->getNumInits()) Init = ILE->getInit(ElementNo++); - - if (Init && isa(Init)) { - if (ZeroInitPadding && - !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, - ZeroFieldSize)) - return false; - + if (Init && isa(Init)) continue; - } // Zero-sized fields are not emitted, but their initializers may still // prevent emission of this struct as a constant. @@ -667,7 +658,7 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { return false; continue; } - + if (ZeroInitPadding && !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, ZeroFieldSize)) From 73436cd195bc6f4d59e8df27ff37f87b9a709fcd Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 15:39:23 +0300 Subject: [PATCH 04/31] use zero index --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 12 ++++++------ clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 8 +++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index d35c6b64671a..2ed3098ab046 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -849,19 +849,19 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { builder.computeGlobalViewIndicesFromFlatOffset(offset, NewTy, layout, newIndx); llvm::SmallVector attrs; - for (auto ind : newIndx) { + for (auto ind : newIndx) { auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(builder.getContext(), 32), ind); attrs.push_back(a); } auto arAttr = mlir::ArrayAttr::get(builder.getContext(), attrs); - auto typ = cir::PointerType::get(builder.getContext(), UCharTy); + auto typ = cir::PointerType::get(builder.getContext(), NewTy); auto newView = builder.getGlobalViewAttr(typ, New, arAttr); glob.setInitialValueAttr(newView); - } + } } - } - } - } + } + } + } } } } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index f27d7bff6c4e..fe7e82bf1ee3 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -653,8 +653,7 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, auto *sourceSymbol = mlir::SymbolTable::lookupSymbolIn(module, globalAttr.getSymbol()); if (auto llvmSymbol = dyn_cast(sourceSymbol)) { - auto typ = dyn_cast(globalAttr.getType()).getPointee(); - sourceType = converter->convertType(typ); + sourceType = llvmSymbol.getType(); symName = llvmSymbol.getSymName(); sourceAddrSpace = llvmSymbol.getAddrSpace(); } else if (auto cirSymbol = dyn_cast(sourceSymbol)) { @@ -684,9 +683,8 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, if (globalAttr.getIndices()) { llvm::SmallVector indices; - if (auto stTy = dyn_cast(sourceType)) { - if (stTy.isIdentified()) - indices.push_back(0); + if (auto stTy = dyn_cast(sourceType)) { + indices.push_back(0); } else if (isa(sourceType)) { indices.push_back(0); } From f2fc318195b39a976673a1c8bca4d9bb3f22960c Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 16:26:53 +0300 Subject: [PATCH 05/31] refactoring --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 60 ++++++++++++++++---------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 2ed3098ab046..1773e9b68a52 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -797,6 +797,40 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { assert(!cir::MissingFeatures::setTargetAttributes()); } +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, + GlobalViewAttr attr, + mlir::Type oldTy, + GlobalOp newGlob) { + + mlir::ArrayAttr indexes = attr.getIndices(); + StructType oldSTy = dyn_cast(oldTy); + + if (!oldSTy || indexes.size() != 1) + llvm_unreachable("NYI"); + + CIRGenBuilderTy &bld = CGM.getBuilder(); + const CIRDataLayout& layout = CGM.getDataLayout(); + mlir::MLIRContext* ctxt = bld.getContext(); + + mlir::IntegerAttr intAttr = dyn_cast(indexes[0]); + int64_t oldInd = intAttr.getValue().getSExtValue(); + mlir::Type newTy = newGlob.getSymType(); + + llvm::SmallVector newInds; + uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + + llvm::SmallVector attrs; + for (auto ind : newInds) { + auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(ctxt, 32), ind); + attrs.push_back(a); + } + + mlir::ArrayAttr arAttr = mlir::ArrayAttr::get(ctxt, attrs); + cir::PointerType typ = cir::PointerType::get(ctxt, newTy); + return bld.getGlobalViewAttr(typ, newGlob, arAttr); +} + void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { assert(Old.getSymName() == New.getSymName() && "symbol names must match"); // If the types does not match, update all references to Old to the new type. @@ -836,29 +870,9 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { } else if (auto glob = dyn_cast(UserOp)) { if (auto init = glob.getInitialValue()) { if (auto attr = mlir::dyn_cast(init.value())) { - if (auto ari = attr.getIndices()) { - auto oldSTy = dyn_cast(OldTy); - if (oldSTy && ari.size() == 1) { - auto intAttr = dyn_cast(ari[0]); - auto oldInd = intAttr.getValue().getSExtValue(); - auto oldSTy = cast(OldTy); - auto& layout = getDataLayout(); - - llvm::SmallVector newIndx; - auto offset = oldSTy.getElementOffset(layout.layout, oldInd); - builder.computeGlobalViewIndicesFromFlatOffset(offset, NewTy, layout, newIndx); - - llvm::SmallVector attrs; - for (auto ind : newIndx) { - auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(builder.getContext(), 32), ind); - attrs.push_back(a); - } - auto arAttr = mlir::ArrayAttr::get(builder.getContext(), attrs); - auto typ = cir::PointerType::get(builder.getContext(), NewTy); - auto newView = builder.getGlobalViewAttr(typ, New, arAttr); - glob.setInitialValueAttr(newView); - } - } + + auto view = createNewGlobalView(*this, attr, OldTy, New); + glob.setInitialValueAttr(view); } } } From 089364d1ecc4c8ecdd9916fa96360e9fa22e27e5 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 25 Dec 2024 16:58:25 +0300 Subject: [PATCH 06/31] handle const arrays --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 1773e9b68a52..70ebda8b8e8f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -869,10 +869,22 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { changeType(GGO); } else if (auto glob = dyn_cast(UserOp)) { if (auto init = glob.getInitialValue()) { - if (auto attr = mlir::dyn_cast(init.value())) { + if (auto attr = mlir::dyn_cast(init.value())) { auto view = createNewGlobalView(*this, attr, OldTy, New); glob.setInitialValueAttr(view); + } else if (auto attr = mlir::dyn_cast(init.value())) { + + llvm::SmallVector newArray; + auto eltsAttr = dyn_cast(attr.getElts()); + for (auto elt : eltsAttr) { + if (auto view = dyn_cast(elt)) { + newArray.push_back(createNewGlobalView(*this, view, OldTy, New)); + } + } + mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); + auto newAr = builder.getConstArray(ar, cast(attr.getType())); + glob.setInitialValueAttr(newAr); } } } From 517a89d695706fdf0e44e80cb965c81b35b8d19b Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 26 Dec 2024 16:23:25 +0300 Subject: [PATCH 07/31] wip --- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 13 +++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 129 +++++++++++++++++++------ 2 files changed, 115 insertions(+), 27 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 46b001531795..1fa581842a29 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -146,6 +146,19 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { return cir::GlobalViewAttr::get(type, symbol, indices); } + cir::GlobalViewAttr getGlobalViewAttr(cir::PointerType type, + cir::GlobalOp globalOp, + llvm::ArrayRef indices) { + llvm::SmallVector attrs; + for (auto ind : indices) { + auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), ind); + attrs.push_back(a); + } + + mlir::ArrayAttr arAttr = mlir::ArrayAttr::get(getContext(), attrs); + return getGlobalViewAttr(type, globalOp, arAttr); + } + mlir::Attribute getString(llvm::StringRef str, mlir::Type eltTy, unsigned size = 0) { unsigned finalSize = size ? size : str.size(); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 70ebda8b8e8f..1efe5c27fd77 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -797,38 +797,112 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { assert(!cir::MissingFeatures::setTargetAttributes()); } -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, - GlobalViewAttr attr, - mlir::Type oldTy, - GlobalOp newGlob) { +static uint64_t offsetOfIndex(const CIRDataLayout& layout, + mlir::Type t, + llvm::ArrayRef indexes) { + if (indexes.empty()) + return 0; + + int64_t index = *indexes.begin(); + mlir::Type elt; + uint64_t offset = 0; + if (auto sTy = dyn_cast(t)) { + offset = sTy.getElementOffset(layout.layout, index); + elt = sTy.getMembers()[index]; + return offset + offsetOfIndex(layout, elt, + llvm::ArrayRef(indexes.begin() + 1, indexes.end())); + } else if (auto arTy = dyn_cast(t)) { + elt = arTy.getEltType(); + int64_t eltSize = layout.getTypeAllocSize(elt); + offset = layout.getTypeAllocSize(elt) * index; + } else { + llvm_unreachable("NYI"); + } - mlir::ArrayAttr indexes = attr.getIndices(); - StructType oldSTy = dyn_cast(oldTy); + return offset + offsetOfIndex(layout, elt, + llvm::ArrayRef(indexes.begin() + 1, indexes.end())); +} - if (!oldSTy || indexes.size() != 1) - llvm_unreachable("NYI"); +// handle view on a struct type +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + cir::StructType oldSTy, + unsigned oldInd) { CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout& layout = CGM.getDataLayout(); mlir::MLIRContext* ctxt = bld.getContext(); - - mlir::IntegerAttr intAttr = dyn_cast(indexes[0]); - int64_t oldInd = intAttr.getValue().getSExtValue(); mlir::Type newTy = newGlob.getSymType(); + cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); llvm::SmallVector newInds; - uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); + // TODO: use the offsetOfIndex function + uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - llvm::SmallVector attrs; - for (auto ind : newInds) { - auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(ctxt, 32), ind); - attrs.push_back(a); - } + return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); +} + +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + GlobalViewAttr attr, + cir::ArrayType oldTy) { - mlir::ArrayAttr arAttr = mlir::ArrayAttr::get(ctxt, attrs); - cir::PointerType typ = cir::PointerType::get(ctxt, newTy); - return bld.getGlobalViewAttr(typ, newGlob, arAttr); +} + +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + GlobalViewAttr attr, + mlir::Type oldTy) { + CIRGenBuilderTy &bld = CGM.getBuilder(); + const CIRDataLayout& layout = CGM.getDataLayout(); + mlir::MLIRContext* ctxt = bld.getContext(); + mlir::Type newTy = newGlob.getSymType(); + + if (!attr.getIndices()) { + cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); + return bld.getGlobalViewAttr(newPtrTy, newGlob); + } + + mlir::ArrayAttr indexes = attr.getIndices(); + cir::StructType oldSTy = dyn_cast(oldTy); + + if (indexes.size() == 1 && oldSTy) { + mlir::IntegerAttr intAttr = dyn_cast(indexes[0]); + int64_t oldInd = intAttr.getValue().getSExtValue(); + return createNewGlobalView(CGM, newGlob, oldSTy, oldInd); + } else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { + cir::ArrayType newArTy = dyn_cast(newTy); + assert(newArTy); + + llvm::SmallVector inds; + std::cout << "OLD inds\n"; + for (auto i : indexes) { + auto ind = dyn_cast(i); + inds.push_back(ind.getValue().getSExtValue()); + + std::cout << " " << inds.back() << '\n'; + } + + auto offset = offsetOfIndex(layout, oldArTy, inds); + llvm::SmallVector newInds; + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + + + std::cout << "newINDS!\n"; + for (auto i : newInds) + std::cout << " " << i << '\n'; + + auto ptrTy = cast(attr.getType()); + + return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); + } + + oldTy.dump(); + attr.dump(); + newGlob.dump(); + + llvm_unreachable("NYI"); } void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { @@ -843,8 +917,7 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { llvm_unreachable("NYI"); } - if (OldTy != NewTy) { - + if (OldTy != NewTy) { auto changeType = [&](GetGlobalOp op) { auto UseOpResultValue = op.getAddr(); UseOpResultValue.setType( @@ -868,18 +941,20 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { if (auto GGO = dyn_cast(Use.getUser())) { changeType(GGO); } else if (auto glob = dyn_cast(UserOp)) { + std::cout << "relplace in user " << glob.getName().str() << "\n"; + glob.dump(); + std::cout << "what init value is?\n"; if (auto init = glob.getInitialValue()) { if (auto attr = mlir::dyn_cast(init.value())) { - auto view = createNewGlobalView(*this, attr, OldTy, New); - glob.setInitialValueAttr(view); - } else if (auto attr = mlir::dyn_cast(init.value())) { - + auto view = createNewGlobalView(*this, New, attr, OldTy); + glob.setInitialValueAttr(view); + } else if (auto attr = mlir::dyn_cast(init.value())) { llvm::SmallVector newArray; auto eltsAttr = dyn_cast(attr.getElts()); for (auto elt : eltsAttr) { if (auto view = dyn_cast(elt)) { - newArray.push_back(createNewGlobalView(*this, view, OldTy, New)); + newArray.push_back(createNewGlobalView(*this, New, view, OldTy)); } } mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); From 9cb7e349672fbb2c0a39003271a31841871f7333 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 26 Dec 2024 18:45:19 +0300 Subject: [PATCH 08/31] wip, refacroting --- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 3 + clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 18 ++- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 146 ++++++++++++---------- 3 files changed, 100 insertions(+), 67 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 1fa581842a29..20dcd98c1caa 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -977,6 +977,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1; Pos = (Pos + AlignMask) & ~AlignMask; assert(Offset >= 0); + if (StructTy.isUnion()) + Pos = 0; + if (Offset < Pos + EltSize) { Indices.push_back(I); SubType = Elts[I]; diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index 8b7e0358a378..e113094b7743 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -9,6 +9,7 @@ // This contains code to emit Constant Expr nodes as LLVM code. // //===----------------------------------------------------------------------===// +#include #include "Address.h" #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" @@ -1385,12 +1386,24 @@ class ConstantLValueEmitter /// Return GEP-like value offset mlir::ArrayAttr getOffset(mlir::Type Ty) { + std::cout << "Check get offset for \n"; + Ty.dump(); + auto Offset = Value.getLValueOffset().getQuantity(); + std::cout << "initial offset is " << Offset << std::endl; + Value.dump(); + cir::CIRDataLayout Layout(CGM.getModule()); SmallVector Idx; CGM.getBuilder().computeGlobalViewIndicesFromFlatOffset(Offset, Ty, Layout, Idx); + std::cout << "got indexes "; + for (auto i : Idx) + std::cout << i << " " ; + std::cout << "\n\n"; + + llvm::SmallVector Indices; for (auto I : Idx) { auto Attr = CGM.getBuilder().getI32IntegerAttr(I); @@ -1413,8 +1426,11 @@ class ConstantLValueEmitter auto baseTy = mlir::cast(GV.getType()).getPointee(); auto destTy = CGM.getTypes().convertTypeForMem(DestType); assert(!GV.getIndices() && "Global view is already indexed"); - return cir::GlobalViewAttr::get(destTy, GV.getSymbol(), + std::cout << "Create globalview 1\n"; + auto x = cir::GlobalViewAttr::get(destTy, GV.getSymbol(), getOffset(baseTy)); + x.dump(); + return x; } llvm_unreachable("Unsupported attribute type to offset"); } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 1efe5c27fd77..0e7521ffbec5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -797,9 +797,10 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { assert(!cir::MissingFeatures::setTargetAttributes()); } +// TODO: move it to CIRGenBuilder static uint64_t offsetOfIndex(const CIRDataLayout& layout, - mlir::Type t, - llvm::ArrayRef indexes) { + mlir::Type t, + llvm::ArrayRef indexes) { if (indexes.empty()) return 0; @@ -807,13 +808,15 @@ static uint64_t offsetOfIndex(const CIRDataLayout& layout, mlir::Type elt; uint64_t offset = 0; if (auto sTy = dyn_cast(t)) { - offset = sTy.getElementOffset(layout.layout, index); + if (sTy.isUnion()) // ??? + offset = 0; + else + offset = sTy.getElementOffset(layout.layout, index); elt = sTy.getMembers()[index]; return offset + offsetOfIndex(layout, elt, llvm::ArrayRef(indexes.begin() + 1, indexes.end())); } else if (auto arTy = dyn_cast(t)) { - elt = arTy.getEltType(); - int64_t eltSize = layout.getTypeAllocSize(elt); + elt = arTy.getEltType(); offset = layout.getTypeAllocSize(elt) * index; } else { llvm_unreachable("NYI"); @@ -843,17 +846,49 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); } -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, +static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { + llvm::SmallVector inds; + + for (auto i : indexes) { + auto ind = dyn_cast(i); + assert(ind); + inds.push_back(ind.getValue().getSExtValue()); + } + + return inds; +} + +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, - cir::ArrayType oldTy) { + cir::ArrayType oldTy, + const llvm::SmallVector& indexes) { + CIRGenBuilderTy &bld = CGM.getBuilder(); + const CIRDataLayout& layout = CGM.getDataLayout(); + mlir::Type newTy = newGlob.getSymType(); + cir::ArrayType newArTy = dyn_cast(newTy); + + assert(newArTy); + + auto offset = offsetOfIndex(layout, oldTy, indexes); + llvm::SmallVector newInds; + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + // TODO: not sure here: may be need new type of index? + auto ptrTy = cast(attr.getType()); + return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); } + static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, mlir::Type oldTy) { +std::cout << ">> createNewGlobalView " << '\n'; +oldTy.dump(); + attr.dump(); + newGlob.dump(); + CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout& layout = CGM.getDataLayout(); mlir::MLIRContext* ctxt = bld.getContext(); @@ -862,47 +897,46 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, if (!attr.getIndices()) { cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); return bld.getGlobalViewAttr(newPtrTy, newGlob); - } - - mlir::ArrayAttr indexes = attr.getIndices(); - cir::StructType oldSTy = dyn_cast(oldTy); - - if (indexes.size() == 1 && oldSTy) { - mlir::IntegerAttr intAttr = dyn_cast(indexes[0]); - int64_t oldInd = intAttr.getValue().getSExtValue(); - return createNewGlobalView(CGM, newGlob, oldSTy, oldInd); - } else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { - cir::ArrayType newArTy = dyn_cast(newTy); - assert(newArTy); - - llvm::SmallVector inds; - std::cout << "OLD inds\n"; - for (auto i : indexes) { - auto ind = dyn_cast(i); - inds.push_back(ind.getValue().getSExtValue()); - - std::cout << " " << inds.back() << '\n'; - } + } - auto offset = offsetOfIndex(layout, oldArTy, inds); - llvm::SmallVector newInds; - bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + auto indexes = indexesOfArrayAttr(attr.getIndices()); + if (indexes.size() == 1 && isa(oldTy)) + return createNewGlobalView(CGM, newGlob, + cast(oldTy), indexes[0]); + else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) + return createNewGlobalView(CGM, newGlob, attr, oldArTy, indexes); + + llvm_unreachable("NYI"); +} - std::cout << "newINDS!\n"; - for (auto i : newInds) - std::cout << " " << i << '\n'; +static void setNewInitValue(CIRGenModule &CGM, + GlobalOp newGlob, + mlir::Type oldTy, + GlobalOp user, + mlir::Attribute oldInit) { + std::cout << "old user\n"; + user.dump(); - auto ptrTy = cast(attr.getType()); + auto& builder = CGM.getBuilder(); - return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); - } - - oldTy.dump(); - attr.dump(); - newGlob.dump(); + if (auto oldView = mlir::dyn_cast(oldInit)) { + auto view = createNewGlobalView(CGM, newGlob, oldView, oldTy); + user.setInitialValueAttr(view); + } else if (auto oldArray = mlir::dyn_cast(oldInit)) { - llvm_unreachable("NYI"); + llvm::SmallVector newArray; + auto eltsAttr = dyn_cast(oldArray.getElts()); + for (auto elt : eltsAttr) + if (auto view = dyn_cast(elt)) + newArray.push_back(createNewGlobalView(CGM, newGlob, view, oldTy)); + + mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); + auto newAr = builder.getConstArray(ar, cast(oldArray.getType())); + user.setInitialValueAttr(newAr); + } else { + llvm_unreachable("NYI"); + } } void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { @@ -938,31 +972,11 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { assert((isa(UserOp) || isa(UserOp)) && "GlobalOp symbol user is neither a GetGlobalOp nor a GlobalOp"); - if (auto GGO = dyn_cast(Use.getUser())) { + if (auto GGO = dyn_cast(Use.getUser())) changeType(GGO); - } else if (auto glob = dyn_cast(UserOp)) { - std::cout << "relplace in user " << glob.getName().str() << "\n"; - glob.dump(); - std::cout << "what init value is?\n"; - if (auto init = glob.getInitialValue()) { - if (auto attr = mlir::dyn_cast(init.value())) { - - auto view = createNewGlobalView(*this, New, attr, OldTy); - glob.setInitialValueAttr(view); - } else if (auto attr = mlir::dyn_cast(init.value())) { - llvm::SmallVector newArray; - auto eltsAttr = dyn_cast(attr.getElts()); - for (auto elt : eltsAttr) { - if (auto view = dyn_cast(elt)) { - newArray.push_back(createNewGlobalView(*this, New, view, OldTy)); - } - } - mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); - auto newAr = builder.getConstArray(ar, cast(attr.getType())); - glob.setInitialValueAttr(newAr); - } - } - } + else if (auto glob = dyn_cast(UserOp)) + if (auto init = glob.getInitialValue()) + setNewInitValue(*this, New, OldTy, glob, init.value()); } } } From f5070b741b19187da9001bbf07c7f8298ef80eea Mon Sep 17 00:00:00 2001 From: gitoleg Date: Sat, 28 Dec 2024 16:20:12 +0300 Subject: [PATCH 09/31] wip --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 18 +----- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 63 ++++++++----------- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 +- .../test/CIR/CodeGen/array-unknown-bound.cpp | 2 +- clang/test/CIR/CodeGen/replace-globals.c | 49 +++++++++++++++ 5 files changed, 80 insertions(+), 57 deletions(-) create mode 100644 clang/test/CIR/CodeGen/replace-globals.c diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index e113094b7743..c3a735ae7bbd 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -1386,24 +1386,11 @@ class ConstantLValueEmitter /// Return GEP-like value offset mlir::ArrayAttr getOffset(mlir::Type Ty) { - std::cout << "Check get offset for \n"; - Ty.dump(); - auto Offset = Value.getLValueOffset().getQuantity(); - std::cout << "initial offset is " << Offset << std::endl; - Value.dump(); - cir::CIRDataLayout Layout(CGM.getModule()); SmallVector Idx; CGM.getBuilder().computeGlobalViewIndicesFromFlatOffset(Offset, Ty, Layout, Idx); - - std::cout << "got indexes "; - for (auto i : Idx) - std::cout << i << " " ; - std::cout << "\n\n"; - - llvm::SmallVector Indices; for (auto I : Idx) { auto Attr = CGM.getBuilder().getI32IntegerAttr(I); @@ -1426,11 +1413,8 @@ class ConstantLValueEmitter auto baseTy = mlir::cast(GV.getType()).getPointee(); auto destTy = CGM.getTypes().convertTypeForMem(DestType); assert(!GV.getIndices() && "Global view is already indexed"); - std::cout << "Create globalview 1\n"; - auto x = cir::GlobalViewAttr::get(destTy, GV.getSymbol(), + return cir::GlobalViewAttr::get(destTy, GV.getSymbol(), getOffset(baseTy)); - x.dump(); - return x; } llvm_unreachable("Unsupported attribute type to offset"); } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 0e7521ffbec5..a28d31587d5c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -798,7 +798,7 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { } // TODO: move it to CIRGenBuilder -static uint64_t offsetOfIndex(const CIRDataLayout& layout, +static uint64_t offsetOfIndex(const CIRDataLayout& layout, mlir::Type t, llvm::ArrayRef indexes) { if (indexes.empty()) @@ -827,20 +827,19 @@ static uint64_t offsetOfIndex(const CIRDataLayout& layout, } -// handle view on a struct type +// handle view on the struct type static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, cir::StructType oldSTy, - unsigned oldInd) { + unsigned oldInd) { CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout& layout = CGM.getDataLayout(); mlir::MLIRContext* ctxt = bld.getContext(); mlir::Type newTy = newGlob.getSymType(); cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); - llvm::SmallVector newInds; - // TODO: use the offsetOfIndex function - uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); + llvm::SmallVector newInds; + uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); @@ -858,7 +857,8 @@ static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { return inds; } -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, +// handle view on the array type +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, cir::ArrayType oldTy, @@ -870,7 +870,7 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, assert(newArTy); - auto offset = offsetOfIndex(layout, oldTy, indexes); + auto offset = offsetOfIndex(layout, oldTy, indexes); llvm::SmallVector newInds; bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); @@ -884,13 +884,7 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, mlir::Type oldTy) { -std::cout << ">> createNewGlobalView " << '\n'; -oldTy.dump(); - attr.dump(); - newGlob.dump(); - CIRGenBuilderTy &bld = CGM.getBuilder(); - const CIRDataLayout& layout = CGM.getDataLayout(); mlir::MLIRContext* ctxt = bld.getContext(); mlir::Type newTy = newGlob.getSymType(); @@ -911,13 +905,10 @@ oldTy.dump(); } static void setNewInitValue(CIRGenModule &CGM, - GlobalOp newGlob, + GlobalOp newGlob, mlir::Type oldTy, GlobalOp user, - mlir::Attribute oldInit) { - std::cout << "old user\n"; - user.dump(); - + mlir::Attribute oldInit) { auto& builder = CGM.getBuilder(); if (auto oldView = mlir::dyn_cast(oldInit)) { @@ -951,20 +942,7 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { llvm_unreachable("NYI"); } - if (OldTy != NewTy) { - auto changeType = [&](GetGlobalOp op) { - auto UseOpResultValue = op.getAddr(); - UseOpResultValue.setType( - cir::PointerType::get(&getMLIRContext(), NewTy)); - - mlir::OpBuilder::InsertionGuard guard(builder); - builder.setInsertionPointAfter(op); - mlir::Type ptrTy = builder.getPointerTo(OldTy); - mlir::Value cast = - builder.createBitcast(op->getLoc(), UseOpResultValue, ptrTy); - UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()}); - }; - + if (OldTy != NewTy) { auto OldSymUses = Old.getSymbolUses(theModule.getOperation()); if (OldSymUses.has_value()) { for (auto Use : *OldSymUses) { @@ -972,11 +950,22 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { assert((isa(UserOp) || isa(UserOp)) && "GlobalOp symbol user is neither a GetGlobalOp nor a GlobalOp"); - if (auto GGO = dyn_cast(Use.getUser())) - changeType(GGO); - else if (auto glob = dyn_cast(UserOp)) + if (auto GGO = dyn_cast(Use.getUser())) { + auto UseOpResultValue = GGO.getAddr(); + UseOpResultValue.setType( + cir::PointerType::get(&getMLIRContext(), NewTy)); + + mlir::OpBuilder::InsertionGuard guard(builder); + builder.setInsertionPointAfter(GGO); + mlir::Type ptrTy = builder.getPointerTo(OldTy); + mlir::Value cast = + builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy); + UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()}); + } + else if (auto glob = dyn_cast(UserOp)) { if (auto init = glob.getInitialValue()) - setNewInitValue(*this, New, OldTy, glob, init.value()); + setNewInitValue(*this, New, OldTy, glob, init.value()); + } } } } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index fe7e82bf1ee3..42a51468d883 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -683,8 +683,9 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, if (globalAttr.getIndices()) { llvm::SmallVector indices; - if (auto stTy = dyn_cast(sourceType)) { - indices.push_back(0); + if (auto stTy = dyn_cast(sourceType)) { + //if (stTy.isIdentified()) + indices.push_back(0); } else if (isa(sourceType)) { indices.push_back(0); } diff --git a/clang/test/CIR/CodeGen/array-unknown-bound.cpp b/clang/test/CIR/CodeGen/array-unknown-bound.cpp index 805b8c5d5867..f026524a80f7 100644 --- a/clang/test/CIR/CodeGen/array-unknown-bound.cpp +++ b/clang/test/CIR/CodeGen/array-unknown-bound.cpp @@ -4,7 +4,7 @@ extern int table[]; // CHECK: cir.global external @table = #cir.const_array<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i]> : !cir.array int *table_ptr = table; -// CHECK: cir.global external @table_ptr = #cir.global_view<@table> : !cir.ptr +// CHECK: cir.global external @table_ptr = #cir.global_view<@table> : !cir.ptr> int test() { return table[1]; } // CHECK: cir.func @_Z4testv() diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/replace-globals.c new file mode 100644 index 000000000000..b0abfb8f29bf --- /dev/null +++ b/clang/test/CIR/CodeGen/replace-globals.c @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +typedef struct { + int f0 : 24; + int f1; + int f2; +} S; + +static S g1 = {2799, 9, 123}; +static int *g2[4] = {&g1.f1, &g1.f1, &g1.f1, &g1.f1}; +static int **g3 = &g2[1]; +static int ***g4 = &g3; +static int ****g5 = &g4; + +static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; +static int *g7[2] = {&g6[0].f2, &g6[1].f2}; +static int **g8 = &g7[1]; + +#include + +int use() { + int a = **g3; + int b = ***g4; + int c = ****g5; + int d = **g8; + + printf("%d %d %d %d\n", a, b, c, d); + + return 0; +} + + +int main() { + use(); + return 0; +} + +// CHECK-DAG: !ty_anon_struct = !cir.struct +// CHECK-DAG: g1 = #cir.const_struct<{#cir.int<239> : !u8i, #cir.int<10> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.int<9> : !s32i, #cir.int<123> : !s32i}> : !ty_anon_struct +// CHECK-DAG: g2 = #cir.const_array<[#cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr]> : !cir.array x 4> +// CHECK-DAG: g3 = #cir.global_view<@g2, [1 : i32]> : !cir.ptr> +// CHECK-DAG: g4 = #cir.global_view<@g3> : !cir.ptr>> +// CHECK-DAG: g5 = #cir.global_view<@g4> : !cir.ptr>>> +// CHECK-DAG: g6 = #cir.const_array<[#cir.const_struct<{#cir.int<239> : !u8i, #cir.int<10> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.int<9> : !s32i, #cir.int<123> : !s32i}> : !ty_anon_struct, #cir.const_struct<{#cir.int<239> : !u8i, #cir.int<10> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.int<9> : !s32i, #cir.int<123> : !s32i}> : !ty_anon_struct]> : !cir.array +// CHECK-DAG: g7 = #cir.const_array<[#cir.global_view<@g6, [0, 5]> : !cir.ptr, #cir.global_view<@g6, [1, 5]> : !cir.ptr]> : !cir.array x 2> +// CHECK-DAG: g8 = #cir.global_view<@g7, [1 : i32]> : !cir.ptr> + + From 30c536f15696c23a732f7dac8be025627364f50a Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 9 Jan 2025 14:46:59 +0300 Subject: [PATCH 10/31] fixed tests, removed zero index for vtt --- clang/lib/CIR/CodeGen/CIRGenVTables.cpp | 3 +-- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 14 +++++-------- clang/test/CIR/CodeGen/replace-globals.c | 21 ++++++++++++------- clang/test/CIR/CodeGen/vbase.cpp | 2 +- clang/test/CIR/CodeGen/vtt.cpp | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp index 2fa51b534da4..81f7971ac787 100644 --- a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp @@ -622,8 +622,7 @@ void CIRGenVTables::emitVTTDefinition(cir::GlobalOp VTT, "Did not find ctor vtable address point!"); } - mlir::Attribute Idxs[3] = { - CGM.getBuilder().getI32IntegerAttr(0), + mlir::Attribute Idxs[2] = { CGM.getBuilder().getI32IntegerAttr(AddressPoint.VTableIndex), CGM.getBuilder().getI32IntegerAttr(AddressPoint.AddressPointIndex), }; diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 42a51468d883..ca99821b80af 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -645,7 +645,7 @@ static mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, mlir::ConversionPatternRewriter &rewriter, const mlir::TypeConverter *converter, - mlir::DataLayout const &dataLayout) { + mlir::DataLayout const &dataLayout) { auto module = parentOp->getParentOfType(); mlir::Type sourceType; unsigned sourceAddrSpace = 0; @@ -683,13 +683,9 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, if (globalAttr.getIndices()) { llvm::SmallVector indices; - if (auto stTy = dyn_cast(sourceType)) { - //if (stTy.isIdentified()) - indices.push_back(0); - } else if (isa(sourceType)) { - indices.push_back(0); - } - + if (isa(sourceType)) + indices.push_back(0); + for (auto idx : globalAttr.getIndices()) { auto intAttr = dyn_cast(idx); assert(intAttr && "index must be integers"); @@ -698,7 +694,7 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, auto resTy = addrOp.getType(); auto eltTy = converter->convertType(sourceType); addrOp = rewriter.create(loc, resTy, eltTy, addrOp, - indices, true); + indices, true); } if (auto intTy = mlir::dyn_cast(globalAttr.getType())) { diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/replace-globals.c index b0abfb8f29bf..65fdf4411abd 100644 --- a/clang/test/CIR/CodeGen/replace-globals.c +++ b/clang/test/CIR/CodeGen/replace-globals.c @@ -1,6 +1,8 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s +//#include + typedef struct { int f0 : 24; int f1; @@ -13,21 +15,26 @@ static int **g3 = &g2[1]; static int ***g4 = &g3; static int ****g5 = &g4; -static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; -static int *g7[2] = {&g6[0].f2, &g6[1].f2}; -static int **g8 = &g7[1]; + static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; + static int *g7[2] = {&g6[0].f2, &g6[1].f2}; + static int **g8 = &g7[1]; + + +// void check() { +// printf("check: %d\n",****g5); +// } -#include int use() { int a = **g3; int b = ***g4; int c = ****g5; - int d = **g8; + int d = **g8; - printf("%d %d %d %d\n", a, b, c, d); + // printf("a: %d, b: %d, c: %d, d: %d\n", a,b,c,d); + //check(); - return 0; + return a + b + c + d; } diff --git a/clang/test/CIR/CodeGen/vbase.cpp b/clang/test/CIR/CodeGen/vbase.cpp index 1ba565b7cb79..574d2d943258 100644 --- a/clang/test/CIR/CodeGen/vbase.cpp +++ b/clang/test/CIR/CodeGen/vbase.cpp @@ -18,7 +18,7 @@ void ppp() { B b; } // CIR: cir.global linkonce_odr @_ZTV1B = #cir.vtable<{#cir.const_array<[#cir.ptr<12 : i64> : !cir.ptr, #cir.ptr : !cir.ptr, #cir.global_view<@_ZTI1B> : !cir.ptr]> : !cir.array x 3>}> // VTT for B. -// CIR: cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 1> +// CIR: cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 1> // CIR: cir.global "private" external @_ZTVN10__cxxabiv121__vmi_class_type_infoE diff --git a/clang/test/CIR/CodeGen/vtt.cpp b/clang/test/CIR/CodeGen/vtt.cpp index ab8cc999f856..c32e242737cf 100644 --- a/clang/test/CIR/CodeGen/vtt.cpp +++ b/clang/test/CIR/CodeGen/vtt.cpp @@ -46,7 +46,7 @@ int f() { // Vtable of Class D // CIR: cir.global linkonce_odr @_ZTV1D = #cir.vtable<{#cir.const_array<[#cir.ptr<40 : i64> : !cir.ptr, #cir.ptr : !cir.ptr, #cir.global_view<@_ZTI1D> : !cir.ptr, #cir.global_view<@_ZN1B1wEv> : !cir.ptr, #cir.global_view<@_ZN1D1yEv> : !cir.ptr]> : !cir.array x 5>, #cir.const_array<[#cir.ptr<24 : i64> : !cir.ptr, #cir.ptr<-16 : i64> : !cir.ptr, #cir.global_view<@_ZTI1D> : !cir.ptr, #cir.global_view<@_ZN1C1xEv> : !cir.ptr]> : !cir.array x 4>, #cir.const_array<[#cir.ptr : !cir.ptr, #cir.ptr<-40 : i64> : !cir.ptr, #cir.global_view<@_ZTI1D> : !cir.ptr, #cir.global_view<@_ZN1A1vEv> : !cir.ptr]> : !cir.array x 4>}> : !ty_anon_struct4 {alignment = 8 : i64} // VTT of class D -// CIR: cir.global linkonce_odr @_ZTT1D = #cir.const_array<[#cir.global_view<@_ZTV1D, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D16_1C, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D16_1C, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTV1D, [0 : i32, 2 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTV1D, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 7> {alignment = 8 : i64} +// CIR: cir.global linkonce_odr @_ZTT1D = #cir.const_array<[#cir.global_view<@_ZTV1D, [0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D0_1B, [1 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D16_1C, [0 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTC1D16_1C, [1 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTV1D, [2 : i32, 3 : i32]> : !cir.ptr, #cir.global_view<@_ZTV1D, [1 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 7> {alignment = 8 : i64} // Class B constructor // CIR: cir.func linkonce_odr @_ZN1BC2Ev(%arg0: !cir.ptr From 1704b3743777aa28d521d30eabdd309a87da5efd Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 9 Jan 2025 15:03:57 +0300 Subject: [PATCH 11/31] adds LLVM checks in tests --- clang/test/CIR/CodeGen/replace-globals.c | 53 ++++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/replace-globals.c index 65fdf4411abd..ef13541353cb 100644 --- a/clang/test/CIR/CodeGen/replace-globals.c +++ b/clang/test/CIR/CodeGen/replace-globals.c @@ -1,7 +1,9 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s - -//#include +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OG typedef struct { int f0 : 24; @@ -15,32 +17,15 @@ static int **g3 = &g2[1]; static int ***g4 = &g3; static int ****g5 = &g4; - static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; - static int *g7[2] = {&g6[0].f2, &g6[1].f2}; - static int **g8 = &g7[1]; - - -// void check() { -// printf("check: %d\n",****g5); -// } - +static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; +static int *g7[2] = {&g6[0].f2, &g6[1].f2}; +static int **g8 = &g7[1]; -int use() { +void use() { int a = **g3; int b = ***g4; int c = ****g5; int d = **g8; - - // printf("a: %d, b: %d, c: %d, d: %d\n", a,b,c,d); - //check(); - - return a + b + c + d; -} - - -int main() { - use(); - return 0; } // CHECK-DAG: !ty_anon_struct = !cir.struct @@ -53,4 +38,26 @@ int main() { // CHECK-DAG: g7 = #cir.const_array<[#cir.global_view<@g6, [0, 5]> : !cir.ptr, #cir.global_view<@g6, [1, 5]> : !cir.ptr]> : !cir.array x 2> // CHECK-DAG: g8 = #cir.global_view<@g7, [1 : i32]> : !cir.ptr> +// LLVM-DAG: @g1 = internal global { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, align 4 +// LLVM-DAG: @g2 = internal global [4 x ptr] [ptr getelementptr inbounds ({ i8, i8, i8, i8, i32, i32 }, ptr @g1, i32 0, i32 4), ptr getelementptr inbounds ({ i8, i8, i8, i8, i32, i32 }, ptr @g1, i32 0, i32 4), ptr getelementptr inbounds ({ i8, i8, i8, i8, i32, i32 }, ptr @g1, i32 0, i32 4), ptr getelementptr inbounds ({ i8, i8, i8, i8, i32, i32 }, ptr @g1, i32 0, i32 4)], align 16 +// LLVM-DAG: @g3 = internal global ptr getelementptr inbounds ([4 x ptr], ptr @g2, i32 0, i32 1), align 8 +// LLVM-DAG: @g4 = internal global ptr @g3, align 8 +// LLVM-DAG: @g5 = internal global ptr @g4, align 8 +// LLVM-DAG: @g6 = internal global [2 x { i8, i8, i8, i8, i32, i32 }] [{ i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }], align 16 +// LLVM-DAG: @g7 = internal global [2 x ptr] [ptr getelementptr inbounds ([2 x { i8, i8, i8, i8, i32, i32 }], ptr @g6, i32 0, i32 0, i32 5), ptr getelementptr inbounds ([2 x { i8, i8, i8, i8, i32, i32 }], ptr @g6, i32 0, i32 1, i32 5)], align 16 +// LLVM-DAG: @g8 = internal global ptr getelementptr inbounds ([2 x ptr], ptr @g7, i32 0, i32 1), align 8 + +// OG-DAG: @g1 = internal global { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, align 4 +// OG-DAG: @g2 = internal global [4 x ptr] [ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4)], align 16 +// OG-DAG: @g3 = internal global ptr getelementptr (i8, ptr @g2, i64 8), align 8 +// OG-DAG: @g4 = internal global ptr @g3, align 8 +// OG-DAG: @g5 = internal global ptr @g4, align 8 +// OG-DAG: @g6 = internal global [2 x { i8, i8, i8, i8, i32, i32 }] [{ i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }], align 16 +// OG-DAG: @g7 = internal global [2 x ptr] [ptr getelementptr (i8, ptr @g6, i64 8), ptr getelementptr (i8, ptr @g6, i64 20)], align 16 +// OG-DAG: @g8 = internal global ptr getelementptr (i8, ptr @g7, i64 8), align 8 + + + + + From 9b9ae91bd90cb390f24774a57263b09967d83626 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 9 Jan 2025 17:10:07 +0300 Subject: [PATCH 12/31] [CIR][CodeGen] handle zero init padding case (#1257) I continue to use `csmith` and catch run time bags. Now it's time to fix the layout for the const structs. There is a divergence between const structs generated by CIR and the original codegen. And this PR makes one more step to eliminate it. There are cases where the extra padding is required - and here is a fix for some of them. I did not write extra tests, since the fixes in the existing already covers the code I added. The point is that now the layout for all of these structs in the LLVM IR with and without CIR is the same. --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index c3a735ae7bbd..86d6cc719cf8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -660,11 +660,6 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { continue; } - if (ZeroInitPadding && - !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, - ZeroFieldSize)) - return false; - if (ZeroInitPadding && !ApplyZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar, ZeroFieldSize)) From 422a663ceae8e3b723189092de6303cdaad8f9dc Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 10 Jan 2025 12:22:52 +0300 Subject: [PATCH 13/31] refactoring wip --- clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 71 ++++++++++++ clang/lib/CIR/CodeGen/CIRGenBuilder.h | 53 ++------- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 1 - clang/lib/CIR/CodeGen/CIRGenModule.cpp | 101 +++++++----------- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 +- 5 files changed, 115 insertions(+), 116 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index a50cefe34c79..58020849d8e0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -63,3 +63,74 @@ cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t, assert(intTy && "expected cir::IntType"); return create(loc, intTy, cir::IntAttr::get(t, C)); } + +void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( + int64_t Offset, mlir::Type Ty, cir::CIRDataLayout Layout, + llvm::SmallVectorImpl &Indices) { + if (!Offset) + return; + + mlir::Type SubType; + + auto getIndexAndNewOffset = + [](int64_t Offset, int64_t EltSize) -> std::pair { + int64_t DivRet = Offset / EltSize; + if (DivRet < 0) + DivRet -= 1; // make sure offset is positive + int64_t ModRet = Offset - (DivRet * EltSize); + return {DivRet, ModRet}; + }; + + if (auto ArrayTy = mlir::dyn_cast(Ty)) { + int64_t EltSize = Layout.getTypeAllocSize(ArrayTy.getEltType()); + SubType = ArrayTy.getEltType(); + auto const [Index, NewOffset] = getIndexAndNewOffset(Offset, EltSize); + Indices.push_back(Index); + Offset = NewOffset; + } else if (auto StructTy = mlir::dyn_cast(Ty)) { + auto Elts = StructTy.getMembers(); + int64_t Pos = 0; + for (size_t I = 0; I < Elts.size(); ++I) { + int64_t EltSize = + (int64_t)Layout.getTypeAllocSize(Elts[I]).getFixedValue(); + unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1; + Pos = (Pos + AlignMask) & ~AlignMask; + assert(Offset >= 0); + if (Offset < Pos + EltSize) { + Indices.push_back(I); + SubType = Elts[I]; + Offset -= Pos; + break; + } + Pos += EltSize; + } + } else { + llvm_unreachable("unexpected type"); + } + + assert(SubType); + computeGlobalViewIndicesFromFlatOffset(Offset, SubType, Layout, Indices); +} + +uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices( + const cir::CIRDataLayout& layout, + mlir::Type t, + llvm::ArrayRef indexes) { + + mlir::Type elt; + uint64_t offset = 0; + for (auto idx : indexes) { + if (auto sTy = dyn_cast(t)) { + offset = sTy.getElementOffset(layout.layout, idx); + assert(idx < sTy.getMembers().size()); + elt = sTy.getMembers()[idx]; + } else if (auto arTy = dyn_cast(t)) { + elt = arTy.getEltType(); + offset = layout.getTypeAllocSize(elt) * idx; + } else { + llvm_unreachable("NYI"); + } + } + + return offset; +} \ No newline at end of file diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 20dcd98c1caa..0bb86231060b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -947,54 +947,13 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { // yet, return them. void computeGlobalViewIndicesFromFlatOffset( int64_t Offset, mlir::Type Ty, cir::CIRDataLayout Layout, - llvm::SmallVectorImpl &Indices) { - if (!Offset) - return; - - mlir::Type SubType; - - auto getIndexAndNewOffset = - [](int64_t Offset, int64_t EltSize) -> std::pair { - int64_t DivRet = Offset / EltSize; - if (DivRet < 0) - DivRet -= 1; // make sure offset is positive - int64_t ModRet = Offset - (DivRet * EltSize); - return {DivRet, ModRet}; - }; - - if (auto ArrayTy = mlir::dyn_cast(Ty)) { - int64_t EltSize = Layout.getTypeAllocSize(ArrayTy.getEltType()); - SubType = ArrayTy.getEltType(); - auto const [Index, NewOffset] = getIndexAndNewOffset(Offset, EltSize); - Indices.push_back(Index); - Offset = NewOffset; - } else if (auto StructTy = mlir::dyn_cast(Ty)) { - auto Elts = StructTy.getMembers(); - int64_t Pos = 0; - for (size_t I = 0; I < Elts.size(); ++I) { - int64_t EltSize = - (int64_t)Layout.getTypeAllocSize(Elts[I]).getFixedValue(); - unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1; - Pos = (Pos + AlignMask) & ~AlignMask; - assert(Offset >= 0); - if (StructTy.isUnion()) - Pos = 0; - - if (Offset < Pos + EltSize) { - Indices.push_back(I); - SubType = Elts[I]; - Offset -= Pos; - break; - } - Pos += EltSize; - } - } else { - llvm_unreachable("unexpected type"); - } + llvm::SmallVectorImpl &Indices); - assert(SubType); - computeGlobalViewIndicesFromFlatOffset(Offset, SubType, Layout, Indices); - } + // Convert high-level indices (e.g. from GlobalViewAttr) to byte offset + uint64_t computeOffsetFromGlobalViewIndices( + const cir::CIRDataLayout& layout, + mlir::Type t, + llvm::ArrayRef indexes); cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) { return create(loc, ty); diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index 86d6cc719cf8..20b43ceedabd 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -9,7 +9,6 @@ // This contains code to emit Constant Expr nodes as LLVM code. // //===----------------------------------------------------------------------===// -#include #include "Address.h" #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index a28d31587d5c..6d0cf4f25705 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -9,7 +9,6 @@ // This is the internal per-translation-unit state used for CIR translation. // //===----------------------------------------------------------------------===// -#include #include "CIRGenModule.h" #include "CIRGenCXXABI.h" @@ -797,46 +796,30 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { assert(!cir::MissingFeatures::setTargetAttributes()); } -// TODO: move it to CIRGenBuilder -static uint64_t offsetOfIndex(const CIRDataLayout& layout, - mlir::Type t, - llvm::ArrayRef indexes) { - if (indexes.empty()) - return 0; - - int64_t index = *indexes.begin(); - mlir::Type elt; - uint64_t offset = 0; - if (auto sTy = dyn_cast(t)) { - if (sTy.isUnion()) // ??? - offset = 0; - else - offset = sTy.getElementOffset(layout.layout, index); - elt = sTy.getMembers()[index]; - return offset + offsetOfIndex(layout, elt, - llvm::ArrayRef(indexes.begin() + 1, indexes.end())); - } else if (auto arTy = dyn_cast(t)) { - elt = arTy.getEltType(); - offset = layout.getTypeAllocSize(elt) * index; - } else { - llvm_unreachable("NYI"); +static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { + llvm::SmallVector inds; + + for (auto i : indexes) { + auto ind = dyn_cast(i); + assert(ind); + inds.push_back(ind.getValue().getSExtValue()); } - return offset + offsetOfIndex(layout, elt, - llvm::ArrayRef(indexes.begin() + 1, indexes.end())); + return inds; } +// handle global view on the struct type +static GlobalViewAttr +createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + cir::StructType oldSTy, + unsigned oldInd) { -// handle view on the struct type -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - cir::StructType oldSTy, - unsigned oldInd) { CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout& layout = CGM.getDataLayout(); mlir::MLIRContext* ctxt = bld.getContext(); mlir::Type newTy = newGlob.getSymType(); - cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); + auto newPtrTy = cir::PointerType::get(ctxt, newTy); llvm::SmallVector newInds; uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); @@ -845,49 +828,39 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); } -static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { - llvm::SmallVector inds; - - for (auto i : indexes) { - auto ind = dyn_cast(i); - assert(ind); - inds.push_back(ind.getValue().getSExtValue()); - } - - return inds; -} - // handle view on the array type -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - GlobalViewAttr attr, - cir::ArrayType oldTy, - const llvm::SmallVector& indexes) { +static GlobalViewAttr +createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + GlobalViewAttr attr, + cir::ArrayType oldTy, + const llvm::SmallVector& indexes) { + CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout& layout = CGM.getDataLayout(); mlir::Type newTy = newGlob.getSymType(); - cir::ArrayType newArTy = dyn_cast(newTy); - + auto newArTy = dyn_cast(newTy); assert(newArTy); - auto offset = offsetOfIndex(layout, oldTy, indexes); + auto offset = bld.computeOffsetFromGlobalViewIndices(layout, oldTy, indexes); llvm::SmallVector newInds; bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - - // TODO: not sure here: may be need new type of index? - auto ptrTy = cast(attr.getType()); + auto ptrTy = dyn_cast(attr.getType()); + assert(ptrTy); + return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); } +static GlobalViewAttr +createNewGlobalView(CIRGenModule &CGM, + GlobalOp newGlob, + GlobalViewAttr attr, + mlir::Type oldTy) { -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - GlobalViewAttr attr, - mlir::Type oldTy) { CIRGenBuilderTy &bld = CGM.getBuilder(); mlir::MLIRContext* ctxt = bld.getContext(); - mlir::Type newTy = newGlob.getSymType(); - + mlir::Type newTy = newGlob.getSymType(); + if (!attr.getIndices()) { cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); return bld.getGlobalViewAttr(newPtrTy, newGlob); @@ -908,8 +881,7 @@ static void setNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, mlir::Type oldTy, GlobalOp user, - mlir::Attribute oldInit) { - auto& builder = CGM.getBuilder(); + mlir::Attribute oldInit) { if (auto oldView = mlir::dyn_cast(oldInit)) { auto view = createNewGlobalView(CGM, newGlob, oldView, oldTy); @@ -921,7 +893,8 @@ static void setNewInitValue(CIRGenModule &CGM, for (auto elt : eltsAttr) if (auto view = dyn_cast(elt)) newArray.push_back(createNewGlobalView(CGM, newGlob, view, oldTy)); - + + auto& builder = CGM.getBuilder(); mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); auto newAr = builder.getConstArray(ar, cast(oldArray.getType())); user.setInitialValueAttr(newAr); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index ca99821b80af..599155a699e9 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -9,9 +9,6 @@ // This file implements lowering of CIR operations to LLVMIR. // //===----------------------------------------------------------------------===// - -#include - #include "LowerToLLVM.h" #include "LoweringHelpers.h" #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" @@ -645,7 +642,7 @@ static mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, mlir::ConversionPatternRewriter &rewriter, const mlir::TypeConverter *converter, - mlir::DataLayout const &dataLayout) { + mlir::DataLayout const &dataLayout) { auto module = parentOp->getParentOfType(); mlir::Type sourceType; unsigned sourceAddrSpace = 0; From 04845100a6cff0a7096698ac9b4078b6331ab2a2 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 10 Jan 2025 12:55:16 +0300 Subject: [PATCH 14/31] bug fixed --- clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index 58020849d8e0..59ffa5c2bb80 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -114,19 +114,18 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices( const cir::CIRDataLayout& layout, - mlir::Type t, - llvm::ArrayRef indexes) { + mlir::Type typ, + llvm::ArrayRef indexes) { - mlir::Type elt; uint64_t offset = 0; for (auto idx : indexes) { - if (auto sTy = dyn_cast(t)) { - offset = sTy.getElementOffset(layout.layout, idx); + if (auto sTy = dyn_cast(typ)) { + offset += sTy.getElementOffset(layout.layout, idx); assert(idx < sTy.getMembers().size()); - elt = sTy.getMembers()[idx]; - } else if (auto arTy = dyn_cast(t)) { - elt = arTy.getEltType(); - offset = layout.getTypeAllocSize(elt) * idx; + typ = sTy.getMembers()[idx]; + } else if (auto arTy = dyn_cast(typ)) { + typ = arTy.getEltType(); + offset += layout.getTypeAllocSize(typ) * idx; } else { llvm_unreachable("NYI"); } From 5ac7673e2ed714594bd74e42de968a73b5c87a6e Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 10 Jan 2025 15:40:32 +0300 Subject: [PATCH 15/31] clang-format ... --- clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 9 +- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 10 +-- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 88 +++++++++---------- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 +- 4 files changed, 51 insertions(+), 60 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index 59ffa5c2bb80..97b63f694bd6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -65,8 +65,8 @@ cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t, } void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( - int64_t Offset, mlir::Type Ty, cir::CIRDataLayout Layout, - llvm::SmallVectorImpl &Indices) { + int64_t Offset, mlir::Type Ty, cir::CIRDataLayout Layout, + llvm::SmallVectorImpl &Indices) { if (!Offset) return; @@ -113,9 +113,8 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( } uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices( - const cir::CIRDataLayout& layout, - mlir::Type typ, - llvm::ArrayRef indexes) { + const cir::CIRDataLayout &layout, mlir::Type typ, + llvm::ArrayRef indexes) { uint64_t offset = 0; for (auto idx : indexes) { diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 0bb86231060b..354117a1f015 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -151,7 +151,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { llvm::ArrayRef indices) { llvm::SmallVector attrs; for (auto ind : indices) { - auto a = mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), ind); + auto a = + mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), ind); attrs.push_back(a); } @@ -950,10 +951,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { llvm::SmallVectorImpl &Indices); // Convert high-level indices (e.g. from GlobalViewAttr) to byte offset - uint64_t computeOffsetFromGlobalViewIndices( - const cir::CIRDataLayout& layout, - mlir::Type t, - llvm::ArrayRef indexes); + uint64_t computeOffsetFromGlobalViewIndices(const cir::CIRDataLayout &layout, + mlir::Type t, + llvm::ArrayRef indexes); cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) { return create(loc, ty); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 6d0cf4f25705..3fe1984d18f9 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -798,8 +798,8 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { llvm::SmallVector inds; - - for (auto i : indexes) { + + for (auto i : indexes) { auto ind = dyn_cast(i); assert(ind); inds.push_back(ind.getValue().getSExtValue()); @@ -809,36 +809,32 @@ static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { } // handle global view on the struct type -static GlobalViewAttr -createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - cir::StructType oldSTy, - unsigned oldInd) { +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, + cir::StructType oldSTy, + unsigned oldInd) { CIRGenBuilderTy &bld = CGM.getBuilder(); - const CIRDataLayout& layout = CGM.getDataLayout(); - mlir::MLIRContext* ctxt = bld.getContext(); + const CIRDataLayout &layout = CGM.getDataLayout(); + mlir::MLIRContext *ctxt = bld.getContext(); mlir::Type newTy = newGlob.getSymType(); - auto newPtrTy = cir::PointerType::get(ctxt, newTy); + auto newPtrTy = cir::PointerType::get(ctxt, newTy); - llvm::SmallVector newInds; + llvm::SmallVector newInds; uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); + return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); } // handle view on the array type -static GlobalViewAttr -createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - GlobalViewAttr attr, +static GlobalViewAttr +createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, cir::ArrayType oldTy, - const llvm::SmallVector& indexes) { + const llvm::SmallVector &indexes) { CIRGenBuilderTy &bld = CGM.getBuilder(); - const CIRDataLayout& layout = CGM.getDataLayout(); - mlir::Type newTy = newGlob.getSymType(); + const CIRDataLayout &layout = CGM.getDataLayout(); + mlir::Type newTy = newGlob.getSymType(); auto newArTy = dyn_cast(newTy); assert(newArTy); @@ -847,59 +843,56 @@ createNewGlobalView(CIRGenModule &CGM, bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); auto ptrTy = dyn_cast(attr.getType()); assert(ptrTy); - + return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); } -static GlobalViewAttr -createNewGlobalView(CIRGenModule &CGM, - GlobalOp newGlob, - GlobalViewAttr attr, - mlir::Type oldTy) { +static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, + GlobalViewAttr attr, + mlir::Type oldTy) { CIRGenBuilderTy &bld = CGM.getBuilder(); - mlir::MLIRContext* ctxt = bld.getContext(); + mlir::MLIRContext *ctxt = bld.getContext(); mlir::Type newTy = newGlob.getSymType(); if (!attr.getIndices()) { - cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); + cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); return bld.getGlobalViewAttr(newPtrTy, newGlob); } - auto indexes = indexesOfArrayAttr(attr.getIndices()); + auto indexes = indexesOfArrayAttr(attr.getIndices()); - if (indexes.size() == 1 && isa(oldTy)) - return createNewGlobalView(CGM, newGlob, - cast(oldTy), indexes[0]); + if (indexes.size() == 1 && isa(oldTy)) + return createNewGlobalView(CGM, newGlob, cast(oldTy), + indexes[0]); else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) - return createNewGlobalView(CGM, newGlob, attr, oldArTy, indexes); - + return createNewGlobalView(CGM, newGlob, attr, oldArTy, indexes); + llvm_unreachable("NYI"); } -static void setNewInitValue(CIRGenModule &CGM, - GlobalOp newGlob, - mlir::Type oldTy, - GlobalOp user, +static void setNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, + mlir::Type oldTy, GlobalOp user, mlir::Attribute oldInit) { if (auto oldView = mlir::dyn_cast(oldInit)) { auto view = createNewGlobalView(CGM, newGlob, oldView, oldTy); user.setInitialValueAttr(view); } else if (auto oldArray = mlir::dyn_cast(oldInit)) { - + llvm::SmallVector newArray; auto eltsAttr = dyn_cast(oldArray.getElts()); for (auto elt : eltsAttr) - if (auto view = dyn_cast(elt)) + if (auto view = dyn_cast(elt)) newArray.push_back(createNewGlobalView(CGM, newGlob, view, oldTy)); - - auto& builder = CGM.getBuilder(); + + auto &builder = CGM.getBuilder(); mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); - auto newAr = builder.getConstArray(ar, cast(oldArray.getType())); + auto newAr = + builder.getConstArray(ar, cast(oldArray.getType())); user.setInitialValueAttr(newAr); } else { - llvm_unreachable("NYI"); + llvm_unreachable("NYI"); } } @@ -933,10 +926,9 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { mlir::Type ptrTy = builder.getPointerTo(OldTy); mlir::Value cast = builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy); - UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()}); - } - else if (auto glob = dyn_cast(UserOp)) { - if (auto init = glob.getInitialValue()) + UseOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp()); + } else if (auto glob = dyn_cast(UserOp)) { + if (auto init = glob.getInitialValue()) setNewInitValue(*this, New, OldTy, glob, init.value()); } } @@ -1192,7 +1184,7 @@ CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *D, mlir::Type Ty, if (!Ty) Ty = getTypes().convertTypeForMem(ASTTy); - auto globalOp = getOrCreateCIRGlobal(D, Ty, IsForDefinition); + auto globalOp = getOrCreateCIRGlobal(D, Ty, IsForDefinition); return builder.getGlobalViewAttr(builder.getPointerTo(Ty), globalOp); } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 599155a699e9..eb32c99549bc 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -681,8 +681,8 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr, llvm::SmallVector indices; if (isa(sourceType)) - indices.push_back(0); - + indices.push_back(0); + for (auto idx : globalAttr.getIndices()) { auto intAttr = dyn_cast(idx); assert(intAttr && "index must be integers"); From 6349b859711e7f6c960d86b8f884075c4a361810 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Mon, 13 Jan 2025 14:09:35 +0300 Subject: [PATCH 16/31] changes after review --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 23 +++++ clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 2 +- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 2 +- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 84 ++++++------------- .../test/CIR/CodeGen/array-unknown-bound.cpp | 2 +- 5 files changed, 52 insertions(+), 61 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 3e12a9307807..ed97b5220951 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -622,6 +622,29 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]> cir.global external @elt_ptr = #cir.global_view<@rgb, [1]> : !cir.ptr cir.global external @table_of_ptrs = #cir.const_array<[#cir.global_view<@rgb, [1]> : !cir.ptr] : !cir.array x 1>> ``` + + Note, that CIR computes indices for vtt slightly different from the LLVM and + doesn't add the leading index since it's always equal to zero. The index is added + later in the lowering. + + Example: + ``` + struct A { + int a; + }; + + struct B: virtual A { + int b; + }; + ``` + VTT for B: + ``` + cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 1> + ``` + The same for LLVM IR after CIR: + ``` + @_ZTT1B = linkonce_odr global [1 x ptr] [ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTV1B, i32 0, i32 0, i32 3)], align 8 + ``` }]; let parameters = (ins AttributeSelfTypeParameter<"">:$type, diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index 97b63f694bd6..db296374c5af 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -114,7 +114,7 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices( const cir::CIRDataLayout &layout, mlir::Type typ, - llvm::ArrayRef indexes) { + llvm::ArrayRef indexes) { uint64_t offset = 0; for (auto idx : indexes) { diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 354117a1f015..7d4f8e9c3acb 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -953,7 +953,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { // Convert high-level indices (e.g. from GlobalViewAttr) to byte offset uint64_t computeOffsetFromGlobalViewIndices(const cir::CIRDataLayout &layout, mlir::Type t, - llvm::ArrayRef indexes); + llvm::ArrayRef indexes); cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) { return create(loc, ty); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 3fe1984d18f9..7698a165e901 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// #include "CIRGenModule.h" - +#include #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" #include "CIRGenFunction.h" @@ -796,77 +796,46 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl GD, mlir::Operation *GO) { assert(!cir::MissingFeatures::setTargetAttributes()); } -static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { - llvm::SmallVector inds; +static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { + llvm::SmallVector inds; - for (auto i : indexes) { + for (mlir::Attribute i : indexes) { auto ind = dyn_cast(i); - assert(ind); + assert(ind && "expect MLIR integer attribute"); inds.push_back(ind.getValue().getSExtValue()); } return inds; } -// handle global view on the struct type -static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, - cir::StructType oldSTy, - unsigned oldInd) { - - CIRGenBuilderTy &bld = CGM.getBuilder(); - const CIRDataLayout &layout = CGM.getDataLayout(); - mlir::MLIRContext *ctxt = bld.getContext(); - mlir::Type newTy = newGlob.getSymType(); - auto newPtrTy = cir::PointerType::get(ctxt, newTy); - - llvm::SmallVector newInds; - uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInd); - bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - - return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); -} - -// handle view on the array type -static GlobalViewAttr -createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, - cir::ArrayType oldTy, - const llvm::SmallVector &indexes) { - - CIRGenBuilderTy &bld = CGM.getBuilder(); - const CIRDataLayout &layout = CGM.getDataLayout(); - mlir::Type newTy = newGlob.getSymType(); - auto newArTy = dyn_cast(newTy); - assert(newArTy); - - auto offset = bld.computeOffsetFromGlobalViewIndices(layout, oldTy, indexes); - llvm::SmallVector newInds; - bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - auto ptrTy = dyn_cast(attr.getType()); - assert(ptrTy); - - return bld.getGlobalViewAttr(ptrTy, newGlob, newInds); -} - static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, mlir::Type oldTy) { - + if (!attr.getIndices()) + return attr; + + llvm::SmallVector oldInds = indexesOfArrayAttr(attr.getIndices()); + llvm::SmallVector newInds; + cir::PointerType newPtrTy; CIRGenBuilderTy &bld = CGM.getBuilder(); + const CIRDataLayout &layout = CGM.getDataLayout(); mlir::MLIRContext *ctxt = bld.getContext(); - mlir::Type newTy = newGlob.getSymType(); - - if (!attr.getIndices()) { - cir::PointerType newPtrTy = cir::PointerType::get(ctxt, newTy); - return bld.getGlobalViewAttr(newPtrTy, newGlob); + auto newTy = newGlob.getSymType(); + + if (oldInds.size() == 1 && isa(oldTy)) { + newPtrTy = cir::PointerType::get(ctxt, newTy); + auto oldSTy = cast(oldTy); + uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInds[0]); + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + } + else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { + auto offset = bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds); + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + newPtrTy = dyn_cast(attr.getType()); } - auto indexes = indexesOfArrayAttr(attr.getIndices()); - - if (indexes.size() == 1 && isa(oldTy)) - return createNewGlobalView(CGM, newGlob, cast(oldTy), - indexes[0]); - else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) - return createNewGlobalView(CGM, newGlob, attr, oldArTy, indexes); + if (newPtrTy) + return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); llvm_unreachable("NYI"); } @@ -879,7 +848,6 @@ static void setNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, auto view = createNewGlobalView(CGM, newGlob, oldView, oldTy); user.setInitialValueAttr(view); } else if (auto oldArray = mlir::dyn_cast(oldInit)) { - llvm::SmallVector newArray; auto eltsAttr = dyn_cast(oldArray.getElts()); for (auto elt : eltsAttr) diff --git a/clang/test/CIR/CodeGen/array-unknown-bound.cpp b/clang/test/CIR/CodeGen/array-unknown-bound.cpp index f026524a80f7..805b8c5d5867 100644 --- a/clang/test/CIR/CodeGen/array-unknown-bound.cpp +++ b/clang/test/CIR/CodeGen/array-unknown-bound.cpp @@ -4,7 +4,7 @@ extern int table[]; // CHECK: cir.global external @table = #cir.const_array<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i]> : !cir.array int *table_ptr = table; -// CHECK: cir.global external @table_ptr = #cir.global_view<@table> : !cir.ptr> +// CHECK: cir.global external @table_ptr = #cir.global_view<@table> : !cir.ptr int test() { return table[1]; } // CHECK: cir.func @_Z4testv() From d45b5f86fc7181b201b8a804db992c4bd5bac383 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Mon, 13 Jan 2025 14:19:37 +0300 Subject: [PATCH 17/31] clang-format ... --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 7698a165e901..5cb76f8e6aa8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -10,7 +10,6 @@ // //===----------------------------------------------------------------------===// #include "CIRGenModule.h" -#include #include "CIRGenCXXABI.h" #include "CIRGenCstEmitter.h" #include "CIRGenFunction.h" @@ -19,6 +18,7 @@ #include "CIRGenTypes.h" #include "CIRGenValue.h" #include "TargetInfo.h" +#include #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" @@ -813,7 +813,7 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, mlir::Type oldTy) { if (!attr.getIndices()) return attr; - + llvm::SmallVector oldInds = indexesOfArrayAttr(attr.getIndices()); llvm::SmallVector newInds; cir::PointerType newPtrTy; @@ -821,15 +821,15 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, const CIRDataLayout &layout = CGM.getDataLayout(); mlir::MLIRContext *ctxt = bld.getContext(); auto newTy = newGlob.getSymType(); - + if (oldInds.size() == 1 && isa(oldTy)) { newPtrTy = cir::PointerType::get(ctxt, newTy); auto oldSTy = cast(oldTy); uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInds[0]); bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - } - else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { - auto offset = bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds); + } else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { + auto offset = + bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds); bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); newPtrTy = dyn_cast(attr.getType()); } From 615011668d0a1ecc9e539d7bc637d68df8568f20 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Mon, 13 Jan 2025 14:53:45 +0300 Subject: [PATCH 18/31] minor --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 5cb76f8e6aa8..589238ff8290 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -18,7 +18,6 @@ #include "CIRGenTypes.h" #include "CIRGenValue.h" #include "TargetInfo.h" -#include #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" From 9d4e0d7f96b378feaa9bf69465fe3c66983e0ed2 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 15 Jan 2025 13:41:56 +0300 Subject: [PATCH 19/31] minor --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 9 +++++---- clang/test/CIR/CodeGen/replace-globals.c | 20 ++++--------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index ed97b5220951..298d06805c54 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -622,11 +622,12 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]> cir.global external @elt_ptr = #cir.global_view<@rgb, [1]> : !cir.ptr cir.global external @table_of_ptrs = #cir.const_array<[#cir.global_view<@rgb, [1]> : !cir.ptr] : !cir.array x 1>> ``` - - Note, that CIR computes indices for vtt slightly different from the LLVM and - doesn't add the leading index since it's always equal to zero. The index is added - later in the lowering. + Note, that unlike LLVM IR's gep instruction, CIR doesn't add the leading zero index + when it's known to be constant zero, e.g. for pointers, i.e. we use indexes exactly + to access sub elements or for the offset. The leading zero index is added later in + the lowering. + Example: ``` struct A { diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/replace-globals.c index ef13541353cb..2f60f5956ec5 100644 --- a/clang/test/CIR/CodeGen/replace-globals.c +++ b/clang/test/CIR/CodeGen/replace-globals.c @@ -2,8 +2,6 @@ // RUN: FileCheck --input-file=%t.cir %s // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll -// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OG typedef struct { int f0 : 24; @@ -47,17 +45,7 @@ void use() { // LLVM-DAG: @g7 = internal global [2 x ptr] [ptr getelementptr inbounds ([2 x { i8, i8, i8, i8, i32, i32 }], ptr @g6, i32 0, i32 0, i32 5), ptr getelementptr inbounds ([2 x { i8, i8, i8, i8, i32, i32 }], ptr @g6, i32 0, i32 1, i32 5)], align 16 // LLVM-DAG: @g8 = internal global ptr getelementptr inbounds ([2 x ptr], ptr @g7, i32 0, i32 1), align 8 -// OG-DAG: @g1 = internal global { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, align 4 -// OG-DAG: @g2 = internal global [4 x ptr] [ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4)], align 16 -// OG-DAG: @g3 = internal global ptr getelementptr (i8, ptr @g2, i64 8), align 8 -// OG-DAG: @g4 = internal global ptr @g3, align 8 -// OG-DAG: @g5 = internal global ptr @g4, align 8 -// OG-DAG: @g6 = internal global [2 x { i8, i8, i8, i8, i32, i32 }] [{ i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }, { i8, i8, i8, i8, i32, i32 } { i8 -17, i8 10, i8 0, i8 0, i32 9, i32 123 }], align 16 -// OG-DAG: @g7 = internal global [2 x ptr] [ptr getelementptr (i8, ptr @g6, i64 8), ptr getelementptr (i8, ptr @g6, i64 20)], align 16 -// OG-DAG: @g8 = internal global ptr getelementptr (i8, ptr @g7, i64 8), align 8 - - - - - - +// FIXME: LLVM output should be: @g2 = internal global [4 x ptr] [ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4), ptr getelementptr (i8, ptr @g1, i64 4)], align 16 +// FIXME: LLVM output should be: @g3 = internal global ptr getelementptr (i8, ptr @g2, i64 8), align 8 +// FIXME: LLVM output should be: @g7 = internal global [2 x ptr] [ptr getelementptr (i8, ptr @g6, i64 8), ptr getelementptr (i8, ptr @g6, i64 20)], align 16 +// FIXME: LLVM output should be: @g8 = internal global ptr getelementptr (i8, ptr @g7, i64 8), align 8 From 54ed36091ddc5d18ad3b752df40bd3e827c716ce Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 15 Jan 2025 13:43:57 +0300 Subject: [PATCH 20/31] minor --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index 20b43ceedabd..b886674b366f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -1385,6 +1385,7 @@ class ConstantLValueEmitter SmallVector Idx; CGM.getBuilder().computeGlobalViewIndicesFromFlatOffset(Offset, Ty, Layout, Idx); + llvm::SmallVector Indices; for (auto I : Idx) { auto Attr = CGM.getBuilder().getI32IntegerAttr(I); From 111c9ac8f70536da03827ea7aed03adaba340daf Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 16 Jan 2025 10:57:13 +0300 Subject: [PATCH 21/31] rm leading zero const idx --- clang/lib/CIR/CodeGen/CIRGenExprConst.cpp | 1 - clang/test/CIR/CodeGen/vtable-emission.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp index b886674b366f..ff987264bc0a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConst.cpp @@ -748,7 +748,6 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, .getAddressPoint(BaseSubobject(CD, Offset)); assert(!cir::MissingFeatures::ptrAuth()); mlir::ArrayAttr indices = builder.getArrayAttr({ - builder.getI32IntegerAttr(0), builder.getI32IntegerAttr(addressPoint.VTableIndex), builder.getI32IntegerAttr(addressPoint.AddressPointIndex), }); diff --git a/clang/test/CIR/CodeGen/vtable-emission.cpp b/clang/test/CIR/CodeGen/vtable-emission.cpp index 6691167488c5..7f09f74de0d5 100644 --- a/clang/test/CIR/CodeGen/vtable-emission.cpp +++ b/clang/test/CIR/CodeGen/vtable-emission.cpp @@ -20,7 +20,7 @@ void S::key() {} // LLVM-SAME: [ptr null, ptr @_ZTI1S, ptr @_ZN1S3keyEv, ptr @_ZN1S6nonKeyEv] }, align 8 // CHECK: cir.global external @sobj = #cir.const_struct -// CHECK-SAME: <{#cir.global_view<@_ZTV1S, [0 : i32, 0 : i32, 2 : i32]> : +// CHECK-SAME: <{#cir.global_view<@_ZTV1S, [0 : i32, 2 : i32]> : // CHECK-SAME: !cir.ptr}> : !ty_anon_struct2 {alignment = 8 : i64} // LLVM: @sobj = global { ptr } { ptr getelementptr inbounds // LLVM-SAME: ({ [4 x ptr] }, ptr @_ZTV1S, i32 0, i32 0, i32 2) }, align 8 From b538bea2505715ce03b3d56a5623523f9c610c0e Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 24 Jan 2025 10:22:56 +0300 Subject: [PATCH 22/31] bug fixing --- clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 2 ++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 3 ++- clang/test/CIR/CodeGen/replace-globals.c | 34 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index db296374c5af..b41220a9d8af 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -94,6 +94,8 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( int64_t EltSize = (int64_t)Layout.getTypeAllocSize(Elts[I]).getFixedValue(); unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1; + if (StructTy.getPacked()) + AlignMask = 0; Pos = (Pos + AlignMask) & ~AlignMask; assert(Offset >= 0); if (Offset < Pos + EltSize) { diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 589238ff8290..addcb50ecae2 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1152,7 +1152,8 @@ CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *D, mlir::Type Ty, Ty = getTypes().convertTypeForMem(ASTTy); auto globalOp = getOrCreateCIRGlobal(D, Ty, IsForDefinition); - return builder.getGlobalViewAttr(builder.getPointerTo(Ty), globalOp); + auto ptrTy = builder.getPointerTo(globalOp.getSymType()); + return builder.getGlobalViewAttr(ptrTy, globalOp); } mlir::Operation *CIRGenModule::getWeakRefReference(const ValueDecl *VD) { diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/replace-globals.c index 2f60f5956ec5..784f84f72ef2 100644 --- a/clang/test/CIR/CodeGen/replace-globals.c +++ b/clang/test/CIR/CodeGen/replace-globals.c @@ -23,7 +23,7 @@ void use() { int a = **g3; int b = ***g4; int c = ****g5; - int d = **g8; + int d = **g8; } // CHECK-DAG: !ty_anon_struct = !cir.struct @@ -49,3 +49,35 @@ void use() { // FIXME: LLVM output should be: @g3 = internal global ptr getelementptr (i8, ptr @g2, i64 8), align 8 // FIXME: LLVM output should be: @g7 = internal global [2 x ptr] [ptr getelementptr (i8, ptr @g6, i64 8), ptr getelementptr (i8, ptr @g6, i64 20)], align 16 // FIXME: LLVM output should be: @g8 = internal global ptr getelementptr (i8, ptr @g7, i64 8), align 8 + +typedef struct { + char f1; + int f6; +} S1; + +S1 g9 = {1, 42}; +int* g10 = &g9.f6; + +#pragma pack(push) +#pragma pack(1) +typedef struct { + char f1; + int f6; +} S2; +#pragma pack(pop) + +S2 g11 = {1, 42}; +int* g12 = &g11.f6; + +// CHECK-DAG: g9 = #cir.const_struct<{#cir.int<1> : !s8i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array, #cir.int<42> : !s32i}> : !ty_anon_struct1 {alignment = 4 : i64} +// CHECK-DAG: g10 = #cir.global_view<@g9, [2 : i32]> : !cir.ptr {alignment = 8 : i64} +// CHECK-DAG: g11 = #cir.const_struct<{#cir.int<1> : !s8i, #cir.int<42> : !s32i}> : !ty_S2_ {alignment = 1 : i64} +// CHECK-DAG: g12 = #cir.global_view<@g11, [1 : i32]> : !cir.ptr {alignment = 8 : i64} + +// LLVM-DAG: @g9 = global { i8, [3 x i8], i32 } { i8 1, [3 x i8] zeroinitializer, i32 42 }, align 4 +// LLVM-DAG: @g10 = global ptr getelementptr inbounds ({ i8, [3 x i8], i32 }, ptr @g9, i32 0, i32 2), align 8 +// LLVM-DAG: @g11 = global %struct.S2 <{ i8 1, i32 42 }>, align 1 +// LLVM-DAG: @g12 = global ptr getelementptr inbounds (%struct.S2, ptr @g11, i32 0, i32 1), align 8 + +// FIXME: LLVM output should be: @g10 = dso_local global ptr getelementptr (i8, ptr @g9, i64 4), align 8 +// FIXME: LLVM output should be: @g12 = dso_local global ptr getelementptr (i8, ptr @g11, i64 1), align 8 From 661e4a4996f82db6bafeca782261c5e655d1ac07 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 24 Jan 2025 10:23:34 +0300 Subject: [PATCH 23/31] renaming --- .../test/CIR/CodeGen/{replace-globals.c => globals-ref-globals.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename clang/test/CIR/CodeGen/{replace-globals.c => globals-ref-globals.c} (100%) diff --git a/clang/test/CIR/CodeGen/replace-globals.c b/clang/test/CIR/CodeGen/globals-ref-globals.c similarity index 100% rename from clang/test/CIR/CodeGen/replace-globals.c rename to clang/test/CIR/CodeGen/globals-ref-globals.c From 7c09a9bb1a53bf3a692bc556b411c8d39b019c75 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Mon, 27 Jan 2025 11:35:07 +0300 Subject: [PATCH 24/31] fixes multi dim array --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index addcb50ecae2..ffac796032ec 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -76,6 +76,7 @@ #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/raw_ostream.h" +#include #include #include @@ -839,25 +840,25 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, llvm_unreachable("NYI"); } -static void setNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, +static mlir::Attribute getNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, mlir::Type oldTy, GlobalOp user, mlir::Attribute oldInit) { - if (auto oldView = mlir::dyn_cast(oldInit)) { - auto view = createNewGlobalView(CGM, newGlob, oldView, oldTy); - user.setInitialValueAttr(view); + return createNewGlobalView(CGM, newGlob, oldView, oldTy); } else if (auto oldArray = mlir::dyn_cast(oldInit)) { llvm::SmallVector newArray; auto eltsAttr = dyn_cast(oldArray.getElts()); - for (auto elt : eltsAttr) + for (auto elt : eltsAttr) { if (auto view = dyn_cast(elt)) newArray.push_back(createNewGlobalView(CGM, newGlob, view, oldTy)); + else if (auto view = dyn_cast(elt)) + newArray.push_back(getNewInitValue(CGM, newGlob, oldTy, user, elt)); + } auto &builder = CGM.getBuilder(); mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); - auto newAr = + return builder.getConstArray(ar, cast(oldArray.getType())); - user.setInitialValueAttr(newAr); } else { llvm_unreachable("NYI"); } @@ -895,8 +896,10 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy); UseOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp()); } else if (auto glob = dyn_cast(UserOp)) { - if (auto init = glob.getInitialValue()) - setNewInitValue(*this, New, OldTy, glob, init.value()); + if (auto init = glob.getInitialValue()) { + auto nw = getNewInitValue(*this, New, OldTy, glob, init.value()); + glob.setInitialValueAttr(nw); + } } } } From 44ac729446230ebbc1aef7715d41c18a0ca19eb9 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Mon, 27 Jan 2025 14:23:53 +0300 Subject: [PATCH 25/31] create new view only for the proper globs --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index ffac796032ec..9e3cd80b5314 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -808,10 +808,14 @@ static llvm::SmallVector indexesOfArrayAttr(mlir::ArrayAttr indexes) { return inds; } +static bool isViewOnGlobal(GlobalOp glob, GlobalViewAttr view) { + return view.getSymbol().getValue() == glob.getSymName(); +} + static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, GlobalViewAttr attr, mlir::Type oldTy) { - if (!attr.getIndices()) + if (!attr.getIndices() || !isViewOnGlobal(newGlob, attr)) return attr; llvm::SmallVector oldInds = indexesOfArrayAttr(attr.getIndices()); @@ -822,7 +826,7 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, mlir::MLIRContext *ctxt = bld.getContext(); auto newTy = newGlob.getSymType(); - if (oldInds.size() == 1 && isa(oldTy)) { + if ( isa(oldTy)) { newPtrTy = cir::PointerType::get(ctxt, newTy); auto oldSTy = cast(oldTy); uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInds[0]); From 85b8ee56351fb6cf4c04a3e261930f6896a097ab Mon Sep 17 00:00:00 2001 From: gitoleg Date: Tue, 28 Jan 2025 15:25:49 +0300 Subject: [PATCH 26/31] fixed bug with nested strucutres --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 9e3cd80b5314..fd4d420383e6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -820,23 +820,19 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, llvm::SmallVector oldInds = indexesOfArrayAttr(attr.getIndices()); llvm::SmallVector newInds; - cir::PointerType newPtrTy; CIRGenBuilderTy &bld = CGM.getBuilder(); const CIRDataLayout &layout = CGM.getDataLayout(); mlir::MLIRContext *ctxt = bld.getContext(); auto newTy = newGlob.getSymType(); - if ( isa(oldTy)) { + auto offset = bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds); + bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + cir::PointerType newPtrTy; + + if ( isa(oldTy)) newPtrTy = cir::PointerType::get(ctxt, newTy); - auto oldSTy = cast(oldTy); - uint64_t offset = oldSTy.getElementOffset(layout.layout, oldInds[0]); - bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); - } else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) { - auto offset = - bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds); - bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); + else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) newPtrTy = dyn_cast(attr.getType()); - } if (newPtrTy) return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds); From c7954a83fb3a3208be1883324ceb0dce5993e393 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 29 Jan 2025 12:51:22 +0300 Subject: [PATCH 27/31] fixes tests --- clang/test/CIR/CodeGen/globals-ref-globals.c | 57 +++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/clang/test/CIR/CodeGen/globals-ref-globals.c b/clang/test/CIR/CodeGen/globals-ref-globals.c index 784f84f72ef2..4c4840816675 100644 --- a/clang/test/CIR/CodeGen/globals-ref-globals.c +++ b/clang/test/CIR/CodeGen/globals-ref-globals.c @@ -19,14 +19,11 @@ static S g6[2] = {{2799, 9, 123}, {2799, 9, 123}}; static int *g7[2] = {&g6[0].f2, &g6[1].f2}; static int **g8 = &g7[1]; -void use() { - int a = **g3; - int b = ***g4; - int c = ****g5; - int d = **g8; -} - // CHECK-DAG: !ty_anon_struct = !cir.struct +// CHECK-DAG: !ty_anon_struct1 = !cir.struct, !s32i}> +// CHECK-DAG: !ty_anon_struct2 = !cir.struct +// CHECK-DAG: !ty_anon_struct3 = !cir.struct, !s32i, !s8i}> + // CHECK-DAG: g1 = #cir.const_struct<{#cir.int<239> : !u8i, #cir.int<10> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.int<9> : !s32i, #cir.int<123> : !s32i}> : !ty_anon_struct // CHECK-DAG: g2 = #cir.const_array<[#cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr]> : !cir.array x 4> // CHECK-DAG: g3 = #cir.global_view<@g2, [1 : i32]> : !cir.ptr> @@ -81,3 +78,49 @@ int* g12 = &g11.f6; // FIXME: LLVM output should be: @g10 = dso_local global ptr getelementptr (i8, ptr @g9, i64 4), align 8 // FIXME: LLVM output should be: @g12 = dso_local global ptr getelementptr (i8, ptr @g11, i64 1), align 8 + + +typedef struct { + short f0; + int f1; + char f2; +} S3; + +static S3 g13 = {-1L,0L,1L}; +static S3* g14[2][2] = {{&g13, &g13}, {&g13, &g13}}; + +// CHECK-DAG: g13 = #cir.const_struct<{#cir.int<-1> : !s16i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array, #cir.int<0> : !s32i, #cir.int<1> : !s8i}> : !ty_anon_struct3 +// CHECK-DAG: g14 = #cir.const_array<[#cir.const_array<[#cir.global_view<@g13> : !cir.ptr, #cir.global_view<@g13> : !cir.ptr]> : !cir.array x 2>, #cir.const_array<[#cir.global_view<@g13> : !cir.ptr, #cir.global_view<@g13> : !cir.ptr]> : !cir.array x 2>]> : !cir.array x 2> x 2> + +typedef struct { + int f0; + int f1; +} S4; + +typedef struct { + int f0 : 17; + int f1 : 5; + int f2 : 19; + S4 f3; +} S5; + +static S5 g15 = {187,1,442,{123,321}}; + +int* g16 = &g15.f3.f1; + +// CHECK-DAG: g15 = #cir.const_struct<{#cir.int<187> : !u8i, #cir.int<0> : !u8i, #cir.int<2> : !u8i, #cir.zero : !u8i, #cir.int<186> : !u8i, #cir.int<1> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.const_struct<{#cir.int<123> : !s32i, #cir.int<321> : !s32i}> : !ty_S4_}> : !ty_anon_struct2 {alignment = 4 : i64} +// CHECK-DAG: g16 = #cir.global_view<@g15, [8, 1]> : !cir.ptr {alignment = 8 : i64} + +// LLVM-DAG: @g15 = internal global { i8, i8, i8, i8, i8, i8, i8, i8, %struct.S4 } { i8 -69, i8 0, i8 2, i8 0, i8 -70, i8 1, i8 0, i8 0, %struct.S4 { i32 123, i32 321 } }, align 4 +// LLVM-DAG: @g16 = global ptr getelementptr inbounds ({ i8, i8, i8, i8, i8, i8, i8, i8, %struct.S4 }, ptr @g15, i32 0, i32 8, i32 1), align 8 + +// FIXME: LLVM output should be: @g16 = dso_local global ptr getelementptr (i8, ptr @g15, i64 12), align 8 + +void use() { + int a = **g3; + int b = ***g4; + int c = ****g5; + int d = **g8; + S3 s = *g14[1][1]; + int f = *g16; +} From a1595a80efd6225b42228324429c5791a42582b9 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 29 Jan 2025 13:02:21 +0300 Subject: [PATCH 28/31] clang-format ... --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index fd4d420383e6..5e21290ec5d7 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -829,7 +829,7 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds); cir::PointerType newPtrTy; - if ( isa(oldTy)) + if (isa(oldTy)) newPtrTy = cir::PointerType::get(ctxt, newTy); else if (cir::ArrayType oldArTy = dyn_cast(oldTy)) newPtrTy = dyn_cast(attr.getType()); @@ -841,8 +841,8 @@ static GlobalViewAttr createNewGlobalView(CIRGenModule &CGM, GlobalOp newGlob, } static mlir::Attribute getNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, - mlir::Type oldTy, GlobalOp user, - mlir::Attribute oldInit) { + mlir::Type oldTy, GlobalOp user, + mlir::Attribute oldInit) { if (auto oldView = mlir::dyn_cast(oldInit)) { return createNewGlobalView(CGM, newGlob, oldView, oldTy); } else if (auto oldArray = mlir::dyn_cast(oldInit)) { @@ -857,8 +857,7 @@ static mlir::Attribute getNewInitValue(CIRGenModule &CGM, GlobalOp newGlob, auto &builder = CGM.getBuilder(); mlir::Attribute ar = mlir::ArrayAttr::get(builder.getContext(), newArray); - return - builder.getConstArray(ar, cast(oldArray.getType())); + return builder.getConstArray(ar, cast(oldArray.getType())); } else { llvm_unreachable("NYI"); } From 28e1772b47c8cc6649d4f996b8423b646bed58ab Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 29 Jan 2025 16:52:09 +0300 Subject: [PATCH 29/31] wip --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 5e21290ec5d7..521c41aa4504 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -76,7 +76,6 @@ #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/raw_ostream.h" -#include #include #include From ff563c37160ea6d94a0122b4fd5754b2c2e9050f Mon Sep 17 00:00:00 2001 From: gitoleg Date: Tue, 4 Feb 2025 13:04:33 +0300 Subject: [PATCH 30/31] minor fix --- clang/lib/CIR/CodeGen/CIRGenBuilder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp index b41220a9d8af..905ca67aafd5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp @@ -84,7 +84,7 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( if (auto ArrayTy = mlir::dyn_cast(Ty)) { int64_t EltSize = Layout.getTypeAllocSize(ArrayTy.getEltType()); SubType = ArrayTy.getEltType(); - auto const [Index, NewOffset] = getIndexAndNewOffset(Offset, EltSize); + const auto [Index, NewOffset] = getIndexAndNewOffset(Offset, EltSize); Indices.push_back(Index); Offset = NewOffset; } else if (auto StructTy = mlir::dyn_cast(Ty)) { From 2f2c84474afdb3baf1f8aeed0a3878d4d8fa205d Mon Sep 17 00:00:00 2001 From: gitoleg Date: Tue, 4 Feb 2025 15:42:14 +0300 Subject: [PATCH 31/31] fixing tests --- clang/test/CIR/CodeGen/globals-ref-globals.c | 4 ++-- clang/test/CIR/CodeGen/union-padding.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/CIR/CodeGen/globals-ref-globals.c b/clang/test/CIR/CodeGen/globals-ref-globals.c index 4c4840816675..8343153e3e8e 100644 --- a/clang/test/CIR/CodeGen/globals-ref-globals.c +++ b/clang/test/CIR/CodeGen/globals-ref-globals.c @@ -22,7 +22,7 @@ static int **g8 = &g7[1]; // CHECK-DAG: !ty_anon_struct = !cir.struct // CHECK-DAG: !ty_anon_struct1 = !cir.struct, !s32i}> // CHECK-DAG: !ty_anon_struct2 = !cir.struct -// CHECK-DAG: !ty_anon_struct3 = !cir.struct, !s32i, !s8i}> +// CHECK-DAG: !ty_anon_struct3 = !cir.struct, !s32i, !s8i, !cir.array}> // CHECK-DAG: g1 = #cir.const_struct<{#cir.int<239> : !u8i, #cir.int<10> : !u8i, #cir.int<0> : !u8i, #cir.zero : !u8i, #cir.int<9> : !s32i, #cir.int<123> : !s32i}> : !ty_anon_struct // CHECK-DAG: g2 = #cir.const_array<[#cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr, #cir.global_view<@g1, [4]> : !cir.ptr]> : !cir.array x 4> @@ -89,7 +89,7 @@ typedef struct { static S3 g13 = {-1L,0L,1L}; static S3* g14[2][2] = {{&g13, &g13}, {&g13, &g13}}; -// CHECK-DAG: g13 = #cir.const_struct<{#cir.int<-1> : !s16i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array, #cir.int<0> : !s32i, #cir.int<1> : !s8i}> : !ty_anon_struct3 +// CHECK-DAG: g13 = #cir.const_struct<{#cir.int<-1> : !s16i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array, #cir.int<0> : !s32i, #cir.int<1> : !s8i, #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array}> : !ty_anon_struct3 // CHECK-DAG: g14 = #cir.const_array<[#cir.const_array<[#cir.global_view<@g13> : !cir.ptr, #cir.global_view<@g13> : !cir.ptr]> : !cir.array x 2>, #cir.const_array<[#cir.global_view<@g13> : !cir.ptr, #cir.global_view<@g13> : !cir.ptr]> : !cir.array x 2>]> : !cir.array x 2> x 2> typedef struct { diff --git a/clang/test/CIR/CodeGen/union-padding.c b/clang/test/CIR/CodeGen/union-padding.c index 8deb2890083c..85ed30d52ce5 100644 --- a/clang/test/CIR/CodeGen/union-padding.c +++ b/clang/test/CIR/CodeGen/union-padding.c @@ -19,7 +19,7 @@ short use() { // CHECK: !ty_anon_struct = !cir.struct}> // CHECK: @g3 = #cir.global_view<@g2> : !cir.ptr> -// CHECK: @g2 = #cir.const_array<[#cir.global_view<@g1, [1 : i32]> : !cir.ptr]> : !cir.array x 1> +// CHECK: @g2 = #cir.const_array<[#cir.global_view<@g1, [1]> : !cir.ptr]> : !cir.array x 1> // CHECK: @g1 = // CHECK-SAME: #cir.const_array<[