Skip to content

Commit 82bfe3c

Browse files
authored
Move to opaque llvm pointers (#8614)
For a long time llvm has been slowly moving to a model where pointers are an opaque type, instead of knowing the pointee type. This has been complete for a while now, so they've started deprecating the APIs for constructing a pointer with a pointee type (these APIs ignore the pointee type anyway), or doing pointer casts, which are no-ops. This PR removes use of those APIs and is a nice simplification of some code.
1 parent a3b2524 commit 82bfe3c

File tree

8 files changed

+51
-97
lines changed

8 files changed

+51
-97
lines changed

src/CodeGen_ARM.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ void CodeGen_ARM::visit(const Store *op) {
15001500
<< (t.is_float() ? 'f' : 'i')
15011501
<< t.bits();
15021502
arg_types = vector<llvm::Type *>(num_vecs + 2, intrin_llvm_type);
1503-
arg_types.front() = PointerType::get(i8_t, 0);
1503+
arg_types.front() = ptr_t;
15041504
arg_types.back() = i32_t;
15051505
} else {
15061506
if (is_sve) {
@@ -1512,7 +1512,7 @@ void CodeGen_ARM::visit(const Store *op) {
15121512
<< t.bits();
15131513
arg_types = vector<llvm::Type *>(num_vecs, intrin_llvm_type);
15141514
arg_types.emplace_back(get_vector_type(i1_t, intrin_type.lanes() / target_vscale(), VectorTypeConstraint::VScale)); // predicate
1515-
arg_types.emplace_back(PointerType::get(llvm_type_of(intrin_type.element_of()), 0));
1515+
arg_types.emplace_back(ptr_t);
15161516
} else {
15171517
instr << "llvm.aarch64.neon.st"
15181518
<< num_vecs
@@ -1522,7 +1522,7 @@ void CodeGen_ARM::visit(const Store *op) {
15221522
<< t.bits()
15231523
<< ".p0";
15241524
arg_types = vector<llvm::Type *>(num_vecs + 1, intrin_llvm_type);
1525-
arg_types.back() = PointerType::get(llvm_type_of(intrin_type.element_of()), 0);
1525+
arg_types.back() = ptr_t;
15261526
}
15271527
}
15281528
llvm::FunctionType *fn_type = FunctionType::get(llvm::Type::getVoidTy(*context), arg_types, false);
@@ -1545,8 +1545,6 @@ void CodeGen_ARM::visit(const Store *op) {
15451545
}
15461546

15471547
if (target.bits == 32) {
1548-
// The arm32 versions take an i8*, regardless of the type stored.
1549-
ptr = builder->CreatePointerCast(ptr, PointerType::get(i8_t, 0));
15501548
// Set the pointer argument
15511549
slice_args.insert(slice_args.begin(), ptr);
15521550
// Set the alignment argument
@@ -1823,7 +1821,7 @@ void CodeGen_ARM::visit(const Load *op) {
18231821
llvm::Type *elt = llvm_type_of(op->type.element_of());
18241822
llvm::Type *slice_type = get_vector_type(elt, slice_lanes);
18251823
StructType *sret_type = StructType::get(module->getContext(), std::vector(stride->value, slice_type));
1826-
std::vector<llvm::Type *> arg_types{get_vector_type(i1_t, slice_lanes), PointerType::get(elt, 0)};
1824+
std::vector<llvm::Type *> arg_types{get_vector_type(i1_t, slice_lanes), ptr_t};
18271825
llvm::FunctionType *fn_type = FunctionType::get(sret_type, arg_types, false);
18281826
FunctionCallee fn = module->getOrInsertFunction(instr.str(), fn_type);
18291827

src/CodeGen_Hexagon.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,10 +2228,6 @@ void CodeGen_Hexagon::visit(const Allocate *alloc) {
22282228
Value *args[2] = {get_user_context(), llvm_size};
22292229

22302230
Value *call = builder->CreateCall(alloc_fn, args);
2231-
2232-
// Fix the type to avoid pointless bitcasts later
2233-
call = builder->CreatePointerCast(
2234-
call, PointerType::get(llvm_type_of(alloc->type), 0));
22352231
allocation.ptr = call;
22362232

22372233
// Assert that the allocation worked.

src/CodeGen_Internal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,10 @@ void set_function_attributes_from_halide_target_options(llvm::Function &fn) {
723723
}
724724

725725
void embed_bitcode(llvm::Module *M, const string &halide_command) {
726-
// Save llvm.compiler.used and remote it.
726+
// Save llvm.compiler.used and remove it.
727727
SmallVector<Constant *, 2> used_array;
728728
SmallVector<GlobalValue *, 4> used_globals;
729-
llvm::Type *used_element_type = PointerType::get(llvm::Type::getInt8Ty(M->getContext()), 0);
729+
llvm::Type *used_element_type = PointerType::get(M->getContext(), 0);
730730
GlobalVariable *used = collectUsedGlobalVariables(*M, used_globals, true);
731731
for (auto *GV : used_globals) {
732732
if (GV->getName() != "llvm.embedded.module" &&

src/CodeGen_LLVM.cpp

Lines changed: 35 additions & 69 deletions
Large diffs are not rendered by default.

src/CodeGen_LLVM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ template<typename, typename>
1818
class IRBuilder;
1919
class LLVMContext;
2020
class Type;
21+
class PointerType;
2122
class StructType;
2223
class Instruction;
2324
class CallInst;
@@ -208,6 +209,7 @@ class CodeGen_LLVM : public IRVisitor {
208209
/** Some useful llvm types */
209210
// @{
210211
llvm::Type *void_t = nullptr, *i1_t = nullptr, *i8_t = nullptr, *i16_t = nullptr, *i32_t = nullptr, *i64_t = nullptr, *f16_t = nullptr, *f32_t = nullptr, *f64_t = nullptr;
212+
llvm::PointerType *ptr_t = nullptr;
211213
llvm::StructType *halide_buffer_t_type = nullptr,
212214
*type_t_type,
213215
*dimension_t_type,

src/CodeGen_PTX_Dev.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void CodeGen_PTX_Dev::add_kernel(Stmt stmt,
138138
vector<llvm::Type *> arg_types(args.size());
139139
for (size_t i = 0; i < args.size(); i++) {
140140
if (args[i].is_buffer) {
141-
arg_types[i] = PointerType::get(llvm_type_of(UInt(8)), 0);
141+
arg_types[i] = ptr_t;
142142
} else {
143143
arg_types[i] = llvm_type_of(args[i].type);
144144
}
@@ -311,7 +311,7 @@ void CodeGen_PTX_Dev::visit(const Allocate *alloc) {
311311
<< "(Memoization is not supported inside GPU kernels at present.)\n";
312312
if (alloc->memory_type == MemoryType::GPUShared) {
313313
// PTX uses zero in address space 3 as the base address for shared memory
314-
Value *shared_base = Constant::getNullValue(PointerType::get(i8_t, 3));
314+
Value *shared_base = Constant::getNullValue(PointerType::get(*context, 3));
315315
sym_push(alloc->name, shared_base);
316316
} else {
317317
debug(2) << "Allocate " << alloc->name << " on device\n";

src/CodeGen_Posix.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ CodeGen_Posix::Allocation CodeGen_Posix::create_allocation(const std::string &na
203203
// allocation that occurs conditionally. TODO: Why not
204204
// just register the destructor at entry?
205205

206-
builder->CreateStore(builder->CreatePointerCast(slot, PointerType::get(i8_t, 0)), allocation.destructor);
206+
builder->CreateStore(slot, allocation.destructor);
207207
free_stack_allocs.erase(it);
208208
} else {
209209
// Stack allocation with a dynamic size
@@ -220,14 +220,11 @@ CodeGen_Posix::Allocation CodeGen_Posix::create_allocation(const std::string &na
220220

221221
llvm::Function::arg_iterator arg_iter = alloc_fn->arg_begin();
222222
++arg_iter; // skip the user context *
223-
slot = builder->CreatePointerCast(slot, arg_iter->getType());
224223
++arg_iter; // skip the pointer to the stack slot
225224
llvm::Type *size_type = arg_iter->getType();
226225
llvm_size = builder->CreateIntCast(llvm_size, size_type, false);
227226
Value *args[3] = {get_user_context(), slot, llvm_size};
228227
Value *call = builder->CreateCall(alloc_fn, args);
229-
llvm::Type *ptr_type = PointerType::get(llvm_type_of(type), 0);
230-
call = builder->CreatePointerCast(call, ptr_type);
231228

232229
// Figure out how much we need to allocate on the real stack
233230
Value *returned_non_null = builder->CreateIsNotNull(call);
@@ -240,21 +237,19 @@ CodeGen_Posix::Allocation CodeGen_Posix::create_allocation(const std::string &na
240237
builder->SetInsertPoint(need_alloca_bb);
241238

242239
// Allocate it. It's zero most of the time.
243-
AllocaInst *alloca_inst = builder->CreateAlloca(PointerType::get(i8_t, 0), llvm_size);
240+
AllocaInst *alloca_inst = builder->CreateAlloca(ptr_t, llvm_size);
244241
// Give it the right alignment
245242
alloca_inst->setAlignment(llvm::Align(native_vector_bits() / 8));
246243

247244
// Set the pseudostack slot ptr to the right thing so we reuse
248245
// this pointer next time around.
249-
Value *stack_ptr = builder->CreatePointerCast(alloca_inst, ptr_type);
250-
Value *slot_ptr_ptr = builder->CreatePointerCast(slot, PointerType::get(ptr_type, 0));
251-
builder->CreateStore(stack_ptr, slot_ptr_ptr);
246+
builder->CreateStore(alloca_inst, slot);
252247

253248
builder->CreateBr(after_bb);
254249
builder->SetInsertPoint(after_bb);
255250

256-
PHINode *phi = builder->CreatePHI(ptr_type, 2);
257-
phi->addIncoming(stack_ptr, need_alloca_bb);
251+
PHINode *phi = builder->CreatePHI(ptr_t, 2);
252+
phi->addIncoming(alloca_inst, need_alloca_bb);
258253
phi->addIncoming(call, here_bb);
259254

260255
allocation.ptr = phi;
@@ -282,9 +277,6 @@ CodeGen_Posix::Allocation CodeGen_Posix::create_allocation(const std::string &na
282277

283278
Value *call = builder->CreateCall(malloc_fn, args);
284279

285-
// Fix the type to avoid pointless bitcasts later
286-
call = builder->CreatePointerCast(call, PointerType::get(llvm_type_of(type), 0));
287-
288280
allocation.ptr = call;
289281
}
290282

src/Func.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ Func Stage::rfactor(const vector<pair<RVar, Var>> &preserved) {
907907

908908
ReductionDomain rdom{definition.schedule().rvars(), definition.predicate(), true};
909909
SubstitutionMap rdom_promises;
910-
for (int i = 0; i < rdom.domain().size(); i++) {
910+
for (int i = 0; i < (int)rdom.domain().size(); i++) {
911911
const auto &[var, min, extent] = rdom.domain()[i];
912912
rdom_promises.emplace(var, promise_clamped(RVar(rdom, i), min, min + extent - 1));
913913
}

0 commit comments

Comments
 (0)