2017-01-29 20:49:31 +00:00
|
|
|
//===------ CGGPUBuiltin.cpp - Codegen for GPU builtins -------------------===//
|
2016-01-23 21:28:14 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-01-23 21:28:14 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-01-29 20:49:31 +00:00
|
|
|
// Generates code for built-in GPU calls which are not runtime-specific.
|
|
|
|
// (Runtime-specific codegen lives in programming model specific files.)
|
2016-01-23 21:28:14 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/Basic/Builtins.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2019-08-22 15:34:35 +05:30
|
|
|
#include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h"
|
2016-01-23 21:28:14 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
namespace {
|
|
|
|
llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
|
2023-11-07 17:26:26 +01:00
|
|
|
llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(M.getContext()),
|
|
|
|
llvm::PointerType::getUnqual(M.getContext())};
|
2016-01-23 21:28:14 +00:00
|
|
|
llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
|
|
|
|
llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
if (auto *F = M.getFunction("vprintf")) {
|
2016-01-23 21:28:14 +00:00
|
|
|
// Our CUDA system header declares vprintf with the right signature, so
|
|
|
|
// nobody else should have been able to declare vprintf with a bogus
|
|
|
|
// signature.
|
|
|
|
assert(F->getFunctionType() == VprintfFuncType);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vprintf doesn't already exist; create a declaration and insert it into the
|
|
|
|
// module.
|
|
|
|
return llvm::Function::Create(
|
|
|
|
VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
llvm::Function *GetOpenMPVprintfDeclaration(CodeGenModule &CGM) {
|
|
|
|
const char *Name = "__llvm_omp_vprintf";
|
|
|
|
llvm::Module &M = CGM.getModule();
|
2023-11-07 17:26:26 +01:00
|
|
|
llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(M.getContext()),
|
|
|
|
llvm::PointerType::getUnqual(M.getContext()),
|
2021-11-10 15:30:55 +00:00
|
|
|
llvm::Type::getInt32Ty(M.getContext())};
|
|
|
|
llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
|
|
|
|
llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
|
|
|
|
|
|
|
|
if (auto *F = M.getFunction(Name)) {
|
|
|
|
if (F->getFunctionType() != VprintfFuncType) {
|
|
|
|
CGM.Error(SourceLocation(),
|
|
|
|
"Invalid type declaration for __llvm_omp_vprintf");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::Function::Create(
|
|
|
|
VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, Name, &M);
|
|
|
|
}
|
|
|
|
|
2016-01-23 21:28:14 +00:00
|
|
|
// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
|
|
|
|
// isn't particularly special; it's invoked just like a regular function).
|
|
|
|
// vprintf takes two args: A format string, and a pointer to a buffer containing
|
|
|
|
// the varargs.
|
|
|
|
//
|
|
|
|
// For example, the call
|
|
|
|
//
|
|
|
|
// printf("format string", arg1, arg2, arg3);
|
|
|
|
//
|
|
|
|
// is converted into something resembling
|
|
|
|
//
|
2016-01-28 23:58:28 +00:00
|
|
|
// struct Tmp {
|
|
|
|
// Arg1 a1;
|
|
|
|
// Arg2 a2;
|
|
|
|
// Arg3 a3;
|
|
|
|
// };
|
|
|
|
// char* buf = alloca(sizeof(Tmp));
|
|
|
|
// *(Tmp*)buf = {a1, a2, a3};
|
2016-01-23 21:28:14 +00:00
|
|
|
// vprintf("format string", buf);
|
|
|
|
//
|
|
|
|
// buf is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of the
|
|
|
|
// args is itself aligned to its preferred alignment.
|
|
|
|
//
|
|
|
|
// Note that by the time this function runs, E's args have already undergone the
|
|
|
|
// standard C vararg promotion (short -> int, float -> double, etc.).
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
std::pair<llvm::Value *, llvm::TypeSize>
|
|
|
|
packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF, const CallArgList &Args) {
|
2021-11-08 14:09:08 +00:00
|
|
|
const llvm::DataLayout &DL = CGF->CGM.getDataLayout();
|
|
|
|
llvm::LLVMContext &Ctx = CGF->CGM.getLLVMContext();
|
|
|
|
CGBuilderTy &Builder = CGF->Builder;
|
2016-02-11 02:00:52 +00:00
|
|
|
|
2016-01-28 23:58:28 +00:00
|
|
|
// Construct and fill the args buffer that we'll pass to vprintf.
|
|
|
|
if (Args.size() <= 1) {
|
2021-11-10 15:30:55 +00:00
|
|
|
// If there are no args, pass a null pointer and size 0
|
2023-11-07 17:26:26 +01:00
|
|
|
llvm::Value *BufferPtr =
|
|
|
|
llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Ctx));
|
2023-11-22 08:52:53 +00:00
|
|
|
return {BufferPtr, llvm::TypeSize::getFixed(0)};
|
2016-01-23 21:28:14 +00:00
|
|
|
} else {
|
2016-01-28 23:58:28 +00:00
|
|
|
llvm::SmallVector<llvm::Type *, 8> ArgTypes;
|
|
|
|
for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
|
2021-11-08 14:09:08 +00:00
|
|
|
ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
|
2016-07-27 22:36:21 +00:00
|
|
|
|
|
|
|
// Using llvm::StructType is correct only because printf doesn't accept
|
|
|
|
// aggregates. If we had to handle aggregates here, we'd have to manually
|
|
|
|
// compute the offsets within the alloca -- we wouldn't be able to assume
|
|
|
|
// that the alignment of the llvm type was the same as the alignment of the
|
|
|
|
// clang type.
|
2016-01-28 23:58:28 +00:00
|
|
|
llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
|
2021-11-08 14:09:08 +00:00
|
|
|
llvm::Value *Alloca = CGF->CreateTempAlloca(AllocaTy);
|
2016-01-23 21:28:14 +00:00
|
|
|
|
|
|
|
for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
|
2016-01-28 23:58:28 +00:00
|
|
|
llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
|
2021-11-08 14:09:08 +00:00
|
|
|
llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
|
2020-01-23 16:18:34 +01:00
|
|
|
Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlign(Arg->getType()));
|
2016-01-23 21:28:14 +00:00
|
|
|
}
|
2021-11-10 15:30:55 +00:00
|
|
|
llvm::Value *BufferPtr =
|
2023-11-07 17:26:26 +01:00
|
|
|
Builder.CreatePointerCast(Alloca, llvm::PointerType::getUnqual(Ctx));
|
2021-11-10 15:30:55 +00:00
|
|
|
return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
|
2016-01-23 21:28:14 +00:00
|
|
|
}
|
2021-11-08 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
[NFC][CLANG] Fix coverity remarks about large copy by values
Reported By Static Analyzer Tool, Coverity:
Big parameter passed by value
Copying large values is inefficient, consider passing by reference; Low, medium, and high size thresholds for detection can be adjusted.
1. Inside "CodeGenModule.cpp" file, in clang::CodeGen::CodeGenModule::EmitBackendOptionsMetadata(clang::CodeGenOptions): A very large function call parameter exceeding the high threshold is passed by value.
pass_by_value: Passing parameter CodeGenOpts of type clang::CodeGenOptions const (size 2168 bytes) by value, which exceeds the high threshold of 512 bytes.
2. Inside "SemaType.cpp" file, in IsNoDerefableChunk(clang::DeclaratorChunk): A large function call parameter exceeding the low threshold is passed by value.
pass_by_value: Passing parameter Chunk of type clang::DeclaratorChunk (size 176 bytes) by value, which exceeds the low threshold of 128 bytes.
3. Inside "CGNonTrivialStruct.cpp" file, in <unnamed>::getParamAddrs<1ull, <0ull...>>(std::integer_sequence<unsigned long long, T2...>, std::array<clang::CharUnits, T1>, clang::CodeGen::FunctionArgList, clang::CodeGen::CodeGenFunction *): A large function call parameter exceeding the low threshold is passed by value.
.i. pass_by_value: Passing parameter Args of type clang::CodeGen::FunctionArgList (size 144 bytes) by value, which exceeds the low threshold of 128 bytes.
4. Inside "CGGPUBuiltin.cpp" file, in <unnamed>::containsNonScalarVarargs(clang::CodeGen::CodeGenFunction *, clang::CodeGen::CallArgList): A very large function call parameter exceeding the high threshold is passed by value.
i. pass_by_value: Passing parameter Args of type clang::CodeGen::CallArgList (size 1176 bytes) by value, which exceeds the high threshold of 512 bytes.
Reviewed By: tahonermann
Differential Revision: https://reviews.llvm.org/D149163
2023-04-28 11:53:43 -07:00
|
|
|
bool containsNonScalarVarargs(CodeGenFunction *CGF, const CallArgList &Args) {
|
2021-11-10 15:30:55 +00:00
|
|
|
return llvm::any_of(llvm::drop_begin(Args), [&](const CallArg &A) {
|
|
|
|
return !A.getRValue(*CGF).isScalar();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue EmitDevicePrintfCallExpr(const CallExpr *E, CodeGenFunction *CGF,
|
|
|
|
llvm::Function *Decl, bool WithSizeArg) {
|
|
|
|
CodeGenModule &CGM = CGF->CGM;
|
|
|
|
CGBuilderTy &Builder = CGF->Builder;
|
2021-11-08 14:09:08 +00:00
|
|
|
assert(E->getBuiltinCallee() == Builtin::BIprintf);
|
|
|
|
assert(E->getNumArgs() >= 1); // printf always has at least one arg.
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
// Uses the same format as nvptx for the argument packing, but also passes
|
|
|
|
// an i32 for the total size of the passed pointer
|
2021-11-08 14:09:08 +00:00
|
|
|
CallArgList Args;
|
2021-11-10 15:30:55 +00:00
|
|
|
CGF->EmitCallArgs(Args,
|
|
|
|
E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
|
|
|
|
E->arguments(), E->getDirectCallee(),
|
|
|
|
/* ParamsToSkip = */ 0);
|
2021-11-08 14:09:08 +00:00
|
|
|
|
|
|
|
// We don't know how to emit non-scalar varargs.
|
2021-11-10 15:30:55 +00:00
|
|
|
if (containsNonScalarVarargs(CGF, Args)) {
|
2021-11-08 14:09:08 +00:00
|
|
|
CGM.ErrorUnsupported(E, "non-scalar arg to printf");
|
2021-11-10 15:30:55 +00:00
|
|
|
return RValue::get(llvm::ConstantInt::get(CGF->IntTy, 0));
|
2021-11-08 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
|
|
|
|
llvm::Value *BufferPtr = r.first;
|
|
|
|
|
|
|
|
llvm::SmallVector<llvm::Value *, 3> Vec = {
|
|
|
|
Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
|
|
|
|
if (WithSizeArg) {
|
|
|
|
// Passing > 32bit of data as a local alloca doesn't work for nvptx or
|
|
|
|
// amdgpu
|
|
|
|
llvm::Constant *Size =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.getLLVMContext()),
|
2023-01-11 15:54:34 +00:00
|
|
|
static_cast<uint32_t>(r.second.getFixedValue()));
|
2016-01-23 21:28:14 +00:00
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
Vec.push_back(Size);
|
|
|
|
}
|
|
|
|
return RValue::get(Builder.CreateCall(Decl, Vec));
|
2016-01-23 21:28:14 +00:00
|
|
|
}
|
2021-11-10 15:30:55 +00:00
|
|
|
} // namespace
|
2019-08-22 15:34:35 +05:30
|
|
|
|
2021-11-10 15:30:55 +00:00
|
|
|
RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E) {
|
|
|
|
assert(getTarget().getTriple().isNVPTX());
|
|
|
|
return EmitDevicePrintfCallExpr(
|
|
|
|
E, this, GetVprintfDeclaration(CGM.getModule()), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
|
2019-08-22 15:34:35 +05:30
|
|
|
assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
|
|
|
|
assert(E->getBuiltinCallee() == Builtin::BIprintf ||
|
|
|
|
E->getBuiltinCallee() == Builtin::BI__builtin_printf);
|
|
|
|
assert(E->getNumArgs() >= 1); // printf always has at least one arg.
|
|
|
|
|
|
|
|
CallArgList CallArgs;
|
|
|
|
EmitCallArgs(CallArgs,
|
|
|
|
E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
|
|
|
|
E->arguments(), E->getDirectCallee(),
|
|
|
|
/* ParamsToSkip = */ 0);
|
|
|
|
|
|
|
|
SmallVector<llvm::Value *, 8> Args;
|
[NFC][clang] Fix Coverity bugs with AUTO_CAUSES_COPY
Reported by Coverity:
AUTO_CAUSES_COPY
Unnecessary object copies can affect performance.
1. Inside "ExtractAPIVisitor.h" file, in clang::extractapi::impl::ExtractAPIVisitorBase<<unnamed>::BatchExtractAPIVisitor>::VisitFunctionDecl(clang::FunctionDecl const *): Using the auto keyword without an & causes the copy of an object of type DynTypedNode.
2. Inside "NeonEmitter.cpp" file, in <unnamed>::Intrinsic::Intrinsic(llvm::Record *, llvm::StringRef, llvm::StringRef, <unnamed>::TypeSpec, <unnamed>::TypeSpec, <unnamed>::ClassKind, llvm::ListInit *, <unnamed>::NeonEmitter &, llvm::StringRef, llvm::StringRef, bool, bool): Using the auto keyword without an & causes the copy of an object of type Type.
3. Inside "MicrosoftCXXABI.cpp" file, in <unnamed>::MSRTTIBuilder::getClassHierarchyDescriptor(): Using the auto keyword without an & causes the copy of an object of type MSRTTIClass.
4. Inside "CGGPUBuiltin.cpp" file, in clang::CodeGen::CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(clang::CallExpr const *): Using the auto keyword without an & causes the copy of an object of type CallArg.
5. Inside "SemaDeclAttr.cpp" file, in threadSafetyCheckIsSmartPointer(clang::Sema &, clang::RecordType const *): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.
6. Inside "ComputeDependence.cpp" file, in clang::computeDependence(clang::DesignatedInitExpr *): Using the auto keyword without an & causes the copy of an object of type Designator.
7. Inside "Format.cpp" file, In clang::format::affectsRange(llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int): Using the auto keyword without an & causes the copy of an object of type Range.
Reviewed By: tahonermann
Differential Revision: https://reviews.llvm.org/D149074
2023-04-24 14:16:32 -07:00
|
|
|
for (const auto &A : CallArgs) {
|
2019-08-22 15:34:35 +05:30
|
|
|
// We don't know how to emit non-scalar varargs.
|
|
|
|
if (!A.getRValue(*this).isScalar()) {
|
|
|
|
CGM.ErrorUnsupported(E, "non-scalar arg to printf");
|
|
|
|
return RValue::get(llvm::ConstantInt::get(IntTy, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *Arg = A.getRValue(*this).getScalarVal();
|
|
|
|
Args.push_back(Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
|
|
|
|
IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
|
2023-05-11 05:00:17 -04:00
|
|
|
|
|
|
|
bool isBuffered = (CGM.getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
|
|
|
|
clang::TargetOptions::AMDGPUPrintfKind::Buffered);
|
|
|
|
auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
|
2019-08-22 15:34:35 +05:30
|
|
|
Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
|
|
|
|
return RValue::get(Printf);
|
|
|
|
}
|
2021-11-10 15:30:55 +00:00
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitOpenMPDevicePrintfCallExpr(const CallExpr *E) {
|
|
|
|
assert(getTarget().getTriple().isNVPTX() ||
|
|
|
|
getTarget().getTriple().isAMDGCN());
|
|
|
|
return EmitDevicePrintfCallExpr(E, this, GetOpenMPVprintfDeclaration(CGM),
|
|
|
|
true);
|
|
|
|
}
|