From cf20ee2269ade924361635ed045083de51821852 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Tue, 7 Apr 2020 14:22:14 -0500 Subject: [PATCH] [LLVM] Use llvm::Align with LLVM 11+ to avoid warnings LLVM 11 is introducing a separate class to represent alignment. The functions in IRBuilder that create aligned loads and stores, and which accept the alignment as an unsigned value have been deprecated (and now cause warnings to be emitted). --- src/target/llvm/codegen_cpu.cc | 32 ++++++++++++++++++++++++++++---- src/target/llvm/codegen_llvm.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/target/llvm/codegen_cpu.cc b/src/target/llvm/codegen_cpu.cc index 33a3e17c939a..f0b0a4b69cf6 100644 --- a/src/target/llvm/codegen_cpu.cc +++ b/src/target/llvm/codegen_cpu.cc @@ -370,7 +370,11 @@ llvm::GlobalVariable* CodeGenCPU::InitContextPtr( llvm::Value* CodeGenCPU::GetContextPtr(llvm::GlobalVariable* gv) { CHECK(gv != nullptr); +#if TVM_LLVM_VERSION >= 110 + llvm::LoadInst* faddr = builder_->CreateAlignedLoad(gv, llvm::Align(gv->getAlignment())); +#else llvm::LoadInst* faddr = builder_->CreateAlignedLoad(gv, gv->getAlignment()); +#endif faddr->setMetadata( "tbaa", md_builder_->createTBAAStructTagNode(md_tbaa_ctx_ptr_, md_tbaa_ctx_ptr_, 0)); @@ -642,7 +646,11 @@ llvm::Value* CodeGenCPU::GetPackedFuncHandle(const std::string& fname) { *ctx_, "handle_init", function_); BasicBlock* end_block = BasicBlock::Create( *ctx_, "handle_init_end", function_); +#if TVM_LLVM_VERSION >= 110 + llvm::Value* handle = builder_->CreateAlignedLoad(hptr, llvm::Align(align)); +#else llvm::Value* handle = builder_->CreateAlignedLoad(hptr, align); +#endif llvm::Value* handle_not_null = builder_->CreateICmpNE( handle, llvm::Constant::getNullValue(t_tvm_func_handle_)); builder_->CreateCondBr( @@ -652,15 +660,24 @@ llvm::Value* CodeGenCPU::GetPackedFuncHandle(const std::string& fname) { llvm::Value* out = WithFunctionEntry([&]() { return builder_->CreateAlloca(t_tvm_func_handle_); }); +#if TVM_LLVM_VERSION >= 110 + llvm::LoadInst* ctx = builder_->CreateAlignedLoad( + gv_mod_ctx_, llvm::Align(gv_mod_ctx_->getAlignment())); +#else llvm::LoadInst* ctx = builder_->CreateAlignedLoad( gv_mod_ctx_, gv_mod_ctx_->getAlignment()); +#endif ctx->setMetadata( "tbaa", md_builder_->createTBAAStructTagNode(md_tbaa_ctx_ptr_, md_tbaa_ctx_ptr_, 0)); llvm::Value* retcode = builder_->CreateCall( RuntimeTVMGetFuncFromEnv(), {ctx, GetConstString(fname), out}); init_block = CheckCallSuccess(retcode); +#if TVM_LLVM_VERSION >= 110 + llvm::Value* loaded_handle = builder_->CreateAlignedLoad(out, llvm::Align(align)); +#else llvm::Value* loaded_handle = builder_->CreateAlignedLoad(out, align); +#endif // Store the handle builder_->CreateStore(loaded_handle, hptr); builder_->CreateBr(end_block); @@ -697,10 +714,13 @@ CodeGenCPU::MakeCallPacked(const Array &args, llvm::Value **rvalue, RuntimeTVMFuncCall(), {handle, arg_value, arg_tcode, ConstInt32(nargs), ret_value, *ret_tcode})); DataType r_api_type = tir::APIType(r_type); - *rvalue = builder_->CreateAlignedLoad( - builder_->CreatePointerCast( - ret_value, DTypeToLLVMType(r_api_type)->getPointerTo()), - 8); + llvm::Value* load_ptr = builder_->CreatePointerCast( + ret_value, DTypeToLLVMType(r_api_type)->getPointerTo()); +#if TVM_LLVM_VERSION >= 110 + *rvalue = builder_->CreateAlignedLoad(load_ptr, llvm::Align(8)); +#else + *rvalue = builder_->CreateAlignedLoad(load_ptr, 8); +#endif *rvalue = CreateCast(r_api_type, r_type, *rvalue); return end_block; } @@ -732,7 +752,11 @@ llvm::Value *CodeGenCPU::CreateCallTracePacked(const CallNode *op) { // traced value. BasicBlock *continue_block = BasicBlock::Create(*ctx_, "continue_block", function_); +#if TVM_LLVM_VERSION >= 110 + llvm::Value *ret_tcode_value = builder_->CreateAlignedLoad(ret_tcode, llvm::Align(8)); +#else llvm::Value *ret_tcode_value = builder_->CreateAlignedLoad(ret_tcode, 8); +#endif // Check the ret_type_code and create cmp instruction. llvm::Value *cmp = builder_->CreateICmpNE( ret_tcode_value, llvm::ConstantInt::get(t_int_, kTVMNullptr)); diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 450ebbcd02b8..bd2cd9f3917f 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -980,7 +980,12 @@ llvm::Value* CodeGenLLVM::VisitExpr_(const LoadNode* op) { int alignment, native_bits; GetAlignment(t, op->buffer_var.get(), op->index, &alignment, &native_bits); llvm::Value* ptr = CreateBufferPtr(t, buffer, index); +#if TVM_LLVM_VERSION >= 110 + llvm::LoadInst* load = + builder_->CreateAlignedLoad(ptr, llvm::Align(alignment), is_volatile); +#else llvm::LoadInst* load = builder_->CreateAlignedLoad(ptr, alignment, is_volatile); +#endif AddAliasInfo(load, op->buffer_var.get(), op->index, t); return load; } else { @@ -996,7 +1001,12 @@ llvm::Value* CodeGenLLVM::VisitExpr_(const LoadNode* op) { t.element_of(), buffer, MakeValue(ramp->base)); ptr = builder_->CreatePointerCast( ptr, DTypeToLLVMType(t)->getPointerTo(addrspace)); +#if TVM_LLVM_VERSION >= 110 + llvm::LoadInst* load = builder_->CreateAlignedLoad( + ptr, llvm::Align(alignment), is_volatile); +#else llvm::LoadInst* load = builder_->CreateAlignedLoad(ptr, alignment, is_volatile); +#endif AddAliasInfo(load, op->buffer_var.get(), op->index, t); return load; } @@ -1007,8 +1017,13 @@ llvm::Value* CodeGenLLVM::VisitExpr_(const LoadNode* op) { llvm::Value* ret = llvm::UndefValue::get(DTypeToLLVMType(t)); auto f = [&](int i, llvm::Value* index) { llvm::Value* ptr = CreateBufferPtr(t.element_of(), buffer, index); +#if TVM_LLVM_VERSION >= 110 + llvm::LoadInst* load = builder_->CreateAlignedLoad( + ptr, llvm::Align(basic_align), is_volatile); +#else llvm::LoadInst* load = builder_->CreateAlignedLoad( ptr, basic_align, is_volatile); +#endif ret = builder_->CreateInsertElement(ret, load, ConstInt32(i)); AddAliasInfo(load, op->buffer_var.get(), PrimExpr(), t); }; @@ -1077,7 +1092,12 @@ void CodeGenLLVM::VisitStmt_(const StoreNode* op) { int alignment, native_bits; GetAlignment(t, op->buffer_var.get(), op->index, &alignment, &native_bits); llvm::Value* ptr = CreateBufferPtr(t, buffer, index); +#if TVM_LLVM_VERSION >= 110 + llvm::StoreInst* store = + builder_->CreateAlignedStore(value, ptr, llvm::Align(alignment), is_volatile); +#else llvm::StoreInst* store = builder_->CreateAlignedStore(value, ptr, alignment, is_volatile); +#endif AddAliasInfo(store, op->buffer_var.get(), op->index, op->value.dtype()); return; } else { @@ -1092,7 +1112,12 @@ void CodeGenLLVM::VisitStmt_(const StoreNode* op) { llvm::Value* ptr = CreateBufferPtr( t.element_of(), buffer, MakeValue(ramp->base)); ptr = builder_->CreatePointerCast(ptr, DTypeToLLVMType(t)->getPointerTo(addrspace)); +#if TVM_LLVM_VERSION >= 110 + llvm::StoreInst* store = + builder_->CreateAlignedStore(value, ptr, llvm::Align(alignment), is_volatile); +#else llvm::StoreInst* store = builder_->CreateAlignedStore(value, ptr, alignment, is_volatile); +#endif AddAliasInfo(store, op->buffer_var.get(), op->index, op->value.dtype()); return; } @@ -1103,9 +1128,15 @@ void CodeGenLLVM::VisitStmt_(const StoreNode* op) { int basic_align = t.bits() / 8; auto f = [&](int i, llvm::Value* index) { llvm::Value* ptr = CreateBufferPtr(t.element_of(), buffer, index); +#if TVM_LLVM_VERSION >= 110 + llvm::StoreInst* store = builder_->CreateAlignedStore( + builder_->CreateExtractElement(value, i), + ptr, llvm::Align(basic_align), is_volatile); +#else llvm::StoreInst* store = builder_->CreateAlignedStore( builder_->CreateExtractElement(value, i), ptr, basic_align, is_volatile); +#endif AddAliasInfo(store, op->buffer_var.get(), PrimExpr(), op->value.dtype()); }; this->Scalarize(op->index, f);