Skip to content

Commit 76b9b59

Browse files
Artem Gindinsonigcbot
authored andcommitted
Enforce default byte alignment in CMABI for LLVM 11+
Starting from LLVM 11, `alloca`/`load`/`store` instructions require an explicit `align` value. At the same time, commit 5eb2fa4fb7 implies that for non-aligned globals, no `align` argument will be set for such user instructions. Since `align 1` is effect-free, use LLVM APIs & IGC's LLVM wrapper to automatically add `align 1` starting from LLVM 11, while retaining the old behavior for earlier LLVM versions.
1 parent 374ec0c commit 76b9b59

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

IGC/VectorCompiler/lib/GenXOpts/CMTrans/CMABI.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ void CMABI::LocalizeGlobals(LocalizationInfo &LI) {
403403

404404
Instruction &FirstI = *Fn->getEntryBlock().begin();
405405
Type *ElemTy = GV->getType()->getElementType();
406+
IGCLLVM::Align GVAlign = IGCLLVM::getCorrectAlign(GV->getAlignment());
406407
AllocaInst *Alloca = new AllocaInst(ElemTy, vc::AddrSpace::Private,
408+
/*ArraySize=*/nullptr, GVAlign,
407409
GV->getName() + ".local", &FirstI);
408410

409-
if (GV->getAlignment())
410-
Alloca->setAlignment(IGCLLVM::getCorrectAlign(GV->getAlignment()));
411-
412411
if (!isa<UndefValue>(GV->getInitializer()))
413-
new StoreInst(GV->getInitializer(), Alloca, &FirstI);
412+
new StoreInst(GV->getInitializer(), Alloca, /*isVolatile=*/false,
413+
GVAlign, &FirstI);
414414

415415
vc::DIBuilder::createDbgDeclareForLocalizedGlobal(*Alloca, *GV, FirstI);
416416

IGC/VectorCompiler/lib/GenXOpts/CMTrans/CMImpParam.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ SPDX-License-Identifier: MIT
147147
#include "llvmWrapper/Analysis/CallGraph.h"
148148
#include "llvmWrapper/IR/DerivedTypes.h"
149149
#include "llvmWrapper/IR/Function.h"
150+
#include "llvmWrapper/Support/Alignment.h"
150151

151152
using namespace llvm;
152153

@@ -775,7 +776,8 @@ void CMImpParam::replaceWithGlobal(CallInst *CI) {
775776
GlobalVariable *GV =
776777
getOrCreateGlobalForIID(CI->getParent()->getParent(), IID);
777778
LoadInst *Load = new LoadInst(GV->getType()->getPointerElementType(), GV, "",
778-
/* isVolatile */ false, CI);
779+
/* isVolatile */ false,
780+
IGCLLVM::getCorrectAlign(GV->getAlignment()), CI);
779781
Load->takeName(CI);
780782
Load->setDebugLoc(CI->getDebugLoc());
781783
CI->replaceAllUsesWith(Load);
@@ -1151,12 +1153,15 @@ CMImpParam::processKernelParameters(Function *F,
11511153
"fewer parameters for new function than expected");
11521154
I2->setName(getImplicitArgName(IID));
11531155

1154-
if (GlobalsMap.count(IID)) {
1156+
auto GlobalsMapIt = GlobalsMap.find(IID);
1157+
if (GlobalsMapIt != GlobalsMap.end()) {
1158+
GlobalVariable *GV = GlobalsMapIt->second;
11551159
// Also insert a new store at the start of the function to the global
11561160
// variable used for this implicit argument intrinsic if such global is
11571161
// present. There are no global for pseudo intrinsics and sometimes for
11581162
// local ID when it is used only for kernel prologue.
1159-
new StoreInst(I2, GlobalsMap[IID], &FirstI);
1163+
new StoreInst(I2, GV, /*isVolatile=*/false,
1164+
IGCLLVM::getCorrectAlign(GV->getAlignment()), &FirstI);
11601165
}
11611166

11621167
// Prepare the kinds that will go into the metadata

IGC/WrapperLLVM/include/llvmWrapper/Support/Alignment.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ namespace IGCLLVM {
5656

5757
inline Align getCorrectAlign(uint32_t Val)
5858
{
59-
if (Val == 0)
60-
{
61-
// LLVM Does not accept 0 alignment.
62-
// Instead assume byte-align.
63-
Val = 1;
64-
}
59+
#if LLVM_VERSION_MAJOR >= 11
60+
// llvm::Align does not accept 0 alignment.
61+
return llvm::assumeAligned(Val);
62+
#else
6563
return Align{ Val };
64+
#endif
6665
}
6766

6867
// It is meant for copying alignement.

0 commit comments

Comments
 (0)