Skip to content

Commit

Permalink
Fix build with LLVM 20.
Browse files Browse the repository at this point in the history
* LLVM 20 renames Intrinsic::getDeclaration to
  Intrinsic::getOrInsertDeclaration. Handle this with a
  multi_llvm::GetOrInsertIntrinsicDeclaration helper function.
* LLVM 20 deprecates instruction creation overloads that take an
  Instruction * as an insertion point, recommending the ones that take
  an iterator instead. In LLVM 18, for the most part, those overloads
  taking iterators do not yet exist, but Instruction::insertBefore does
  already take iterators, so rework the code to consistently use that.
  This removes some bitcast instructions in memory_operations.cpp that
  could not be updated, but had become unnecessary ever since LLVM
  switched to opaque pointers.
  • Loading branch information
hvdijk committed Oct 15, 2024
1 parent 1d3a925 commit e82f8d3
Show file tree
Hide file tree
Showing 31 changed files with 399 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <llvm/IR/Module.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/Utils/ValueMapper.h>
#include <multi_llvm/multi_llvm.h>

#define DEBUG_TYPE "add-sched-params"

Expand Down Expand Up @@ -232,7 +231,8 @@ PreservedAnalyses compiler::utils::AddSchedulingParametersPass::run(
NewArgs,
ArrayRef(FArgs.data(), FArgs.size()).take_back(NumSchedParams));

auto *NewCB = CallInst::Create(NewF, NewArgs, "", CB);
auto *NewCB = CallInst::Create(NewF, NewArgs);
NewCB->insertBefore(CB->getIterator());
NewCB->takeName(CB);
NewCB->copyMetadata(*CB);
// Copy over all the old attributes from the call
Expand Down
11 changes: 5 additions & 6 deletions modules/compiler/compiler_pipeline/source/barrier_regions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/Utils/LCSSA.h>
#include <llvm/Transforms/Utils/Local.h>
#include <multi_llvm/multi_llvm.h>
#include <multi_llvm/vector_type_helper.h>

#include <optional>

Expand Down Expand Up @@ -580,14 +580,12 @@ void compiler::utils::Barrier::SplitBlockwithBarrier() {
auto id = ConstantInt::get(Type::getInt32Ty(module_.getContext()),
barrier_id - kBarrier_StartNewID);
// Call invoking entry stub
auto entry_caller =
CallInst::Create(entry_stub, id, "", (Instruction *)nullptr);
auto entry_caller = CallInst::Create(entry_stub, id);
entry_caller->setDebugLoc(split_point->getDebugLoc());
entry_caller->setCallingConv(entry_stub->getCallingConv());

// Call invoking exit stub
auto exit_caller =
CallInst::Create(exit_stub, id, "", (Instruction *)nullptr);
auto exit_caller = CallInst::Create(exit_stub, id);
exit_caller->setDebugLoc(split_point->getDebugLoc());
exit_caller->setCallingConv(exit_stub->getCallingConv());

Expand Down Expand Up @@ -1163,7 +1161,8 @@ Function *compiler::utils::Barrier::GenerateNewKernel(BarrierRegion &region) {
// Change return instruction with end barrier number.
ConstantInt *cst_zero =
ConstantInt::get(Type::getInt32Ty(context), kBarrier_EndID);
ReturnInst *new_ret = ReturnInst::Create(context, cst_zero, ret);
ReturnInst *new_ret = ReturnInst::Create(context, cst_zero);
new_ret->insertBefore(ret->getIterator());
ret->replaceAllUsesWith(new_ret);
ret->eraseFromParent();

Expand Down
3 changes: 2 additions & 1 deletion modules/compiler/compiler_pipeline/source/builtin_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <llvm/ADT/StringExtras.h>
#include <llvm/ADT/StringSwitch.h>
#include <llvm/IR/Module.h>
#include <multi_llvm/intrinsic.h>

using namespace llvm;

Expand Down Expand Up @@ -538,7 +539,7 @@ Function *BuiltinInfo::getScalarEquivalent(const Builtin &B, Module *M) {
Type *ScalarType = VecRetTy->getElementType();
// Get the scalar version of the intrinsic
Function *ScalarIntrinsic =
Intrinsic::getDeclaration(M, IntrinsicID, ScalarType);
multi_llvm::GetOrInsertIntrinsicDeclaration(M, IntrinsicID, ScalarType);

return ScalarIntrinsic;
}
Expand Down
17 changes: 10 additions & 7 deletions modules/compiler/compiler_pipeline/source/cl_builtin_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <llvm/TargetParser/Triple.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/Utils/ValueMapper.h>
#include <multi_llvm/multi_llvm.h>
#include <multi_llvm/vector_type_helper.h>

#include <cmath>
Expand Down Expand Up @@ -2807,7 +2806,8 @@ Instruction *CLBuiltinInfo::lowerBuiltinToMuxBuiltin(
auto *const MuxBuiltinFn = BIMuxImpl.getOrDeclareMuxBuiltin(*MuxID, M);
assert(MuxBuiltinFn && "Could not get/declare mux builtin");
const SmallVector<Value *> Args(CI.args());
auto *const NewCI = CallInst::Create(MuxBuiltinFn, Args, CI.getName(), &CI);
auto *const NewCI = CallInst::Create(MuxBuiltinFn, Args, CI.getName());
NewCI->insertBefore(CI.getIterator());
NewCI->takeName(&CI);
NewCI->setAttributes(MuxBuiltinFn->getAttributes());
return NewCI;
Expand Down Expand Up @@ -3344,9 +3344,9 @@ Instruction *CLBuiltinInfo::lowerGroupBuiltinToMuxBuiltin(
Args.push_back(Val);
} else {
assert(Val->getType()->isIntegerTy());
auto *NEZero =
ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, Val,
ConstantInt::getNullValue(Val->getType()), "", &CI);
auto *NEZero = ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, Val,
ConstantInt::getNullValue(Val->getType()));
NEZero->insertBefore(CI.getIterator());
Args.push_back(NEZero);
}

Expand All @@ -3363,15 +3363,18 @@ Instruction *CLBuiltinInfo::lowerGroupBuiltinToMuxBuiltin(
}
}

auto *const NewCI = CallInst::Create(MuxBuiltinFn, Args, CI.getName(), &CI);
auto *const NewCI = CallInst::Create(MuxBuiltinFn, Args, CI.getName());
NewCI->insertBefore(CI.getIterator());
NewCI->takeName(&CI);
NewCI->setAttributes(MuxBuiltinFn->getAttributes());

if (!IsAnyAll) {
return NewCI;
}
// For any/all we need to recreate the original i32 return value.
return SExtInst::Create(Instruction::SExt, NewCI, CI.getType(), "sext", &CI);
auto *SExt = SExtInst::Create(Instruction::SExt, NewCI, CI.getType(), "sext");
SExt->insertBefore(CI.getIterator());
return SExt;
}

Instruction *CLBuiltinInfo::lowerAsyncBuiltinToMuxBuiltin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ void replaceSubgroupBuiltinCall(CallInst *CI,
for (auto &arg : CI->args()) {
Args.push_back(arg);
}
auto *WGCI = CallInst::Create(WorkGroupBuiltinFn, Args, "", CI);
auto *WGCI = CallInst::Create(WorkGroupBuiltinFn, Args);
WGCI->insertBefore(CI->getIterator());
WGCI->setCallingConv(CI->getCallingConv());
CI->replaceAllUsesWith(WGCI);
return;
Expand Down Expand Up @@ -217,11 +218,13 @@ void replaceSubgroupWorkItemBuiltinCall(CallInst *CI,
compiler::utils::eMuxBuiltinGetLocalLinearId, *M);
GetLocalLinearID->setCallingConv(CI->getCallingConv());
auto *const LocalLinearIDCall =
CallInst::Create(GetLocalLinearID, {}, "", CI);
CallInst::Create(GetLocalLinearID, ArrayRef<Value *>{});
LocalLinearIDCall->insertBefore(CI->getIterator());
LocalLinearIDCall->setCallingConv(CI->getCallingConv());
auto *const LocalLinearID = CastInst::CreateIntegerCast(
LocalLinearIDCall, Type::getInt32Ty(M->getContext()),
/* isSigned */ false, "", CI);
/* isSigned */ false);
LocalLinearID->insertBefore(CI->getIterator());
CI->replaceAllUsesWith(LocalLinearID);
} else {
llvm_unreachable("unhandled sub-group builtin function");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Module.h>
#include <llvm/TargetParser/Triple.h>
#include <multi_llvm/intrinsic.h>
#include <multi_llvm/vector_type_helper.h>

#define DEBUG_TYPE "ca-optimal-builtins"
Expand Down Expand Up @@ -61,7 +62,8 @@ Value *OptimalBuiltinReplacementPass::replaceAbacusCLZ(
SmallVector<Value *, 4> Args(CB.args());
// Get the declaration for the intrinsic
auto *const ArgTy = Args[0]->getType();
auto *const Intrinsic = Intrinsic::getDeclaration(M, Intrinsic::ctlz, ArgTy);
auto *const Intrinsic =
multi_llvm::GetOrInsertIntrinsicDeclaration(M, Intrinsic::ctlz, ArgTy);
// If we didn't find the intrinsic or the return type isn't what we
// expect, skip this optimization
Function *Callee = CB.getCalledFunction();
Expand All @@ -82,7 +84,9 @@ Value *OptimalBuiltinReplacementPass::replaceAbacusCLZ(
LLVMContext &Ctx = M->getContext();
Args.push_back(ConstantInt::getFalse(Ctx));

return CallInst::Create(Intrinsic, Args, "", &CB);
auto *Call = CallInst::Create(Intrinsic, Args);
Call->insertBefore(CB.getIterator());
return Call;
}

Value *OptimalBuiltinReplacementPass::replaceAbacusMulhi(
Expand Down
4 changes: 2 additions & 2 deletions modules/compiler/compiler_pipeline/source/pass_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <llvm/IR/Module.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <multi_llvm/llvm_version.h>
#include <multi_llvm/multi_llvm.h>
#include <multi_llvm/vector_type_helper.h>

#include <cassert>
Expand Down Expand Up @@ -440,7 +439,8 @@ void remapClonedCallsites(llvm::Function &oldFunc, llvm::Function &newFunc,
}

// create our new call instruction to replace the old one
auto newCi = llvm::CallInst::Create(&newFunc, args, name, ci);
auto newCi = llvm::CallInst::Create(&newFunc, args, name);
newCi->insertBefore(ci->getIterator());

// use the debug location from the old call (if any)
newCi->setDebugLoc(ci->getDebugLoc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ GetElementPtrInst *generateStructGEP(Instruction &inst,
// create a new GEP just before the instruction
auto GEP = GetElementPtrInst::CreateInBounds(
funcsStructTy, funcsStruct,
{ConstantInt::get(indexTy, 0), ConstantInt::get(indexTy, index)}, "",
&inst);
{ConstantInt::get(indexTy, 0), ConstantInt::get(indexTy, index)});
GEP->insertBefore(inst.getIterator());
return GEP;
}

Expand Down Expand Up @@ -436,29 +436,34 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local = generateStructGEP(*gep, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", gep);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(gep->getIterator());

gep->setOperand(0, castedLocal);
gep->setIsInBounds();
} else if (CastInst *cast = dyn_cast<CastInst>(user)) {
auto local = generateStructGEP(*cast, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", cast);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(cast->getIterator());

cast->setOperand(0, castedLocal);
} else if (LoadInst *load = dyn_cast<LoadInst>(user)) {
auto local = generateStructGEP(*load, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", load);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(load->getIterator());

load->setOperand(0, castedLocal);
} else if (StoreInst *store = dyn_cast<StoreInst>(user)) {
auto local = generateStructGEP(*store, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", store);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(store->getIterator());

// global could be pointer or value operand of the store
if (store->getValueOperand() == global) {
store->setOperand(0, castedLocal);
Expand All @@ -474,22 +479,25 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local = generateStructGEP(*inst, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", inst);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(inst->getIterator());

auto indexTy = Type::getInt32Ty(M.getContext());
Value *newCv = UndefValue::get(cv->getType());

// We can't simply 'setOperand' in a 'ConstantVector'. We have to
// recreate it from scratch.
for (unsigned i = 0; i < cv->getNumOperands(); ++i) {
Instruction *newCvInst;
if (cv->getOperand(i) == global) {
newCv = InsertElementInst::Create(
newCv, castedLocal, ConstantInt::get(indexTy, i), "", inst);
newCvInst = InsertElementInst::Create(
newCv, castedLocal, ConstantInt::get(indexTy, i));
} else {
newCv = InsertElementInst::Create(newCv, cv->getOperand(i),
ConstantInt::get(indexTy, i),
"", inst);
newCvInst = InsertElementInst::Create(
newCv, cv->getOperand(i), ConstantInt::get(indexTy, i));
}
newCvInst->insertBefore(inst->getIterator());
newCv = newCvInst;
}

// And don't forget to replace 'cv' by 'newCv'.
Expand All @@ -505,8 +513,9 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local =
generateStructGEP(*incomingBlockT, structTy, index_map[global]);

auto castedLocal = CastInst::CreatePointerCast(
local, global->getType(), "", incomingBlockT);
auto castedLocal =
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(incomingBlockT->getIterator());

phi->setIncomingValue(i, castedLocal);
}
Expand All @@ -515,7 +524,8 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local = generateStructGEP(*atomic, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", atomic);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(atomic->getIterator());

// global could be pointer or value operand of the atomic
if (atomic->getPointerOperand() == global) {
Expand All @@ -527,7 +537,8 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
const auto local =
generateStructGEP(*atomic, structTy, index_map[global]);
const auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", atomic);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(atomic->getIterator());

// global could be the pointer
if (atomic->getPointerOperand() == global) {
Expand All @@ -545,7 +556,8 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local = generateStructGEP(*select, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", select);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(select->getIterator());

// global could be the true or false value of the select
if (select->getTrueValue() == global) {
Expand All @@ -557,7 +569,8 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
auto local = generateStructGEP(*call, structTy, index_map[global]);

auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", call);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(call->getIterator());

unsigned i = 0;
for (; i < call->getNumOperands(); ++i) {
Expand All @@ -568,15 +581,19 @@ PreservedAnalyses compiler::utils::ReplaceLocalModuleScopeVariablesPass::run(
} else if (InsertElementInst *insertIns =
dyn_cast<InsertElementInst>(user)) {
auto local = generateStructGEP(*insertIns, structTy, index_map[global]);
auto castedLocal = CastInst::CreatePointerCast(local, global->getType(),
"", insertIns);
auto castedLocal =
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(insertIns->getIterator());

// Update middle operand as the others are the vector and index
insertIns->setOperand(1, castedLocal);
} else if (auto *cmpIns = dyn_cast<CmpInst>(user)) {
const auto local =
generateStructGEP(*cmpIns, structTy, index_map[global]);
const auto castedLocal =
CastInst::CreatePointerCast(local, global->getType(), "", cmpIns);
CastInst::CreatePointerCast(local, global->getType());
castedLocal->insertBefore(cmpIns->getIterator());

// global could be either side of the compare
if (cmpIns->getOperand(0) == global) {
cmpIns->setOperand(0, castedLocal);
Expand Down
Loading

0 comments on commit e82f8d3

Please sign in to comment.