Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions clang/include/clang/Basic/SyclOptReportHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ class SyclOptReportHandler {
std::string KernelArgName;
std::string KernelArgType;
SourceLocation KernelArgLoc;
unsigned KernelArgSize;
std::string KernelArgDesc;
std::string KernelArgParent;

OptReportInfo(std::string ArgName, std::string ArgType,
SourceLocation ArgLoc)
SourceLocation ArgLoc, unsigned ArgSize, std::string ArgDesc,
std::string ArgParent)
: KernelArgName(std::move(ArgName)), KernelArgType(std::move(ArgType)),
KernelArgLoc(ArgLoc) {}
KernelArgLoc(ArgLoc), KernelArgSize(ArgSize),
KernelArgDesc(std::move(ArgDesc)),
KernelArgParent(std::move(ArgParent)) {}
};
llvm::DenseMap<const FunctionDecl *, SmallVector<OptReportInfo>> Map;

public:
void AddKernelArgs(const FunctionDecl *FD, std::string ArgName,
std::string ArgType, SourceLocation ArgLoc) {
Map[FD].emplace_back(ArgName, ArgType, ArgLoc);
std::string ArgType, SourceLocation ArgLoc,
unsigned ArgSize, std::string ArgDesc,
std::string ArgParent) {
Map[FD].emplace_back(ArgName, ArgType, ArgLoc, ArgSize, ArgDesc, ArgParent);
}
SmallVector<OptReportInfo> &GetInfo(const FunctionDecl *FD) {
auto It = Map.find(FD);
Expand Down
58 changes: 52 additions & 6 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,33 @@ QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD,
return ResTy;
}

enum class ArgDescEncoding {
BaseClass,
DecomposedMember,
WrappedPointer,
WrappedArray,
Accessor,
AccessorBase,
Sampler,
Stream,
KernelHandler,
None
};

static ArgDescEncoding getArgDescEncoding(std::string KernelArgDesc) {
return llvm::StringSwitch<ArgDescEncoding>(KernelArgDesc)
.Case("base class", ArgDescEncoding::BaseClass)
.Case("decomposed struct/class", ArgDescEncoding::DecomposedMember)
.Case("nested pointer", ArgDescEncoding::WrappedPointer)
.Case("array", ArgDescEncoding::WrappedArray)
.Case("accessor", ArgDescEncoding::Accessor)
.Case("accessor base class", ArgDescEncoding::AccessorBase)
.Case("sampler", ArgDescEncoding::Sampler)
.Case("stream", ArgDescEncoding::Stream)
.Case("SYCL2020 specialization constant", ArgDescEncoding::KernelHandler)
.Default(ArgDescEncoding::None);
}

void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
const CGFunctionInfo &FnInfo) {
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Expand Down Expand Up @@ -1518,14 +1545,33 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
for (auto ORI : llvm::enumerate(OptReportHandler.GetInfo(FD))) {
llvm::DiagnosticLocation DL =
SourceLocToDebugLoc(ORI.value().KernelArgLoc);
std::string KAN = ORI.value().KernelArgName;
std::string ArgName = ORI.value().KernelArgName;
std::string ArgType = ORI.value().KernelArgType;
std::string ArgDesc = ORI.value().KernelArgDesc;
unsigned ArgSize = ORI.value().KernelArgSize;
std::string ArgParent = ORI.value().KernelArgParent;

ArgDescEncoding ArgDescEnc = getArgDescEncoding(ArgDesc);
bool isWrappedField = (ArgDescEnc == ArgDescEncoding::WrappedPointer ||
ArgDescEnc == ArgDescEncoding::WrappedArray)
? true
: false;

llvm::OptimizationRemark Remark("sycl", "Region", DL,
&Fn->getEntryBlock());
Remark << "Argument " << llvm::ore::NV("Argument", ORI.index())
<< " for function kernel: "
<< llvm::ore::NV(KAN.empty() ? "&" : "") << " " << Fn->getName()
<< "." << llvm::ore::NV(KAN.empty() ? " " : KAN) << "("
<< ORI.value().KernelArgType << ")";
Remark << "Arg " << llvm::ore::NV("Argument", ORI.index()) << ":"
<< ((ArgDescEnc != ArgDescEncoding::None)
? ("Compiler generated argument for " + ArgDesc + ",")
: "")
<< ((ArgDescEnc == ArgDescEncoding::DecomposedMember) ? ArgParent
: ArgName)
<< " ("
<< ((ArgDescEnc == ArgDescEncoding::DecomposedMember)
? ("Field:" + ArgName + ", ")
: "")
<< "Type:" << ((isWrappedField) ? "Compiler generated" : ArgType)
<< ", "
<< "Size: " << llvm::ore::NV("Argument", ArgSize) << ")";
ORE.emit(Remark);
}
}
Expand Down
198 changes: 191 additions & 7 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,9 +1768,6 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {

void addParam(const FieldDecl *FD, QualType FieldTy) {
ParamDesc newParamDesc = makeParamDesc(FD, FieldTy);
SemaRef.getDiagnostics().getSYCLOptReportHandler().AddKernelArgs(
KernelDecl, FD->getName().data(), FieldTy.getAsString(),
FD->getLocation());
addParam(newParamDesc, FieldTy);
}

Expand All @@ -1781,8 +1778,6 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {
StringRef Name = "_arg__base";
ParamDesc newParamDesc =
makeParamDesc(SemaRef.getASTContext(), Name, FieldTy);
SemaRef.getDiagnostics().getSYCLOptReportHandler().AddKernelArgs(
KernelDecl, "", FieldTy.getAsString(), BS.getBaseTypeLoc());
addParam(newParamDesc, FieldTy);
}
// Add a parameter with specified name and type
Expand Down Expand Up @@ -2230,6 +2225,193 @@ class SyclKernelArgsSizeChecker : public SyclKernelFieldHandler {
using SyclKernelFieldHandler::handleSyclHalfType;
};

enum class KernelArgDescription {
BaseClass,
DecomposedMember,
WrappedPointer,
WrappedArray,
Accessor,
AccessorBase,
Sampler,
Stream,
KernelHandler,
None
};

StringRef getKernelArgDesc(KernelArgDescription Desc) {
switch (Desc) {
case KernelArgDescription::BaseClass:
return "base class";
break;
case KernelArgDescription::DecomposedMember:
return "decomposed struct/class";
break;
case KernelArgDescription::WrappedPointer:
return "nested pointer";
break;
case KernelArgDescription::WrappedArray:
return "array";
break;
case KernelArgDescription::Accessor:
return "accessor";
break;
case KernelArgDescription::AccessorBase:
return "accessor base class";
break;
case KernelArgDescription::Sampler:
return "sampler";
break;
case KernelArgDescription::Stream:
return "stream";
break;
case KernelArgDescription::KernelHandler:
return "SYCL2020 specialization constant";
break;
default:
return "";
}
}

class SyclOptReportCreator : public SyclKernelFieldHandler {
SyclKernelDeclCreator &DC;
SourceLocation KernelInvocationLoc;

void addParam(const FieldDecl *KernelArg, QualType KernelArgType,
KernelArgDescription KernelArgDesc) {
unsigned KernelArgSize =
SemaRef.getASTContext().getTypeSizeInChars(KernelArgType).getQuantity();
const RecordDecl *KernelArgParent = KernelArg->getParent();
SemaRef.getDiagnostics().getSYCLOptReportHandler().AddKernelArgs(
DC.getKernelDecl(), KernelArg->getName().data(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once AddKernalArgs() takes a StringRef, you can simplify this call.

KernelArgType.getAsString(), KernelInvocationLoc, KernelArgSize,
getKernelArgDesc(KernelArgDesc).data(),
KernelArgParent ? KernelArgParent->getName().data() : "");
}

void addParam(const FieldDecl *FD, QualType FieldTy) {
KernelArgDescription Desc = KernelArgDescription::None;
const RecordDecl *RD = FD->getParent();
if (RD && RD->hasAttr<SYCLRequiresDecompositionAttr>())
Desc = KernelArgDescription::DecomposedMember;

addParam(FD, FieldTy, Desc);
}

void addParam(QualType KernelArgType, KernelArgDescription KernelArgDesc) {
unsigned KernelArgSize =
SemaRef.getASTContext().getTypeSizeInChars(KernelArgType).getQuantity();
SemaRef.getDiagnostics().getSYCLOptReportHandler().AddKernelArgs(
DC.getKernelDecl(), KernelArgType.getAsString(),
KernelArgType.getAsString(), KernelInvocationLoc, KernelArgSize,
getKernelArgDesc(KernelArgDesc).data(), "");
}

// Handles SYCL special types - accessor, sampler and stream
// Also handles modified types - arrays and pointers
bool handleSpecialType(const FieldDecl *FD, QualType FieldTy,
KernelArgDescription Desc) {
llvm::ArrayRef<ParmVarDecl *> KernelParameters =
DC.getParamVarDeclsForCurrentField();
for (auto *Param : KernelParameters)
addParam(FD, Param->getType(), Desc);
return true;
}

public:
static constexpr const bool VisitInsideSimpleContainers = false;
SyclOptReportCreator(Sema &S, SyclKernelDeclCreator &DC, SourceLocation Loc)
: SyclKernelFieldHandler(S), DC(DC), KernelInvocationLoc(Loc) {}

bool handleSyclAccessorType(FieldDecl *FD, QualType FieldTy) final {
return handleSpecialType(
FD, FieldTy, KernelArgDescription(KernelArgDescription::Accessor));
}

bool handleSyclAccessorType(const CXXRecordDecl *, const CXXBaseSpecifier &BS,
QualType FieldTy) final {
llvm::ArrayRef<ParmVarDecl *> KernelParameters =
DC.getParamVarDeclsForCurrentField();
for (auto *Param : KernelParameters)
addParam(Param->getType(),
KernelArgDescription(KernelArgDescription::AccessorBase));
return true;
}

bool handleSyclSamplerType(FieldDecl *FD, QualType FieldTy) final {
return handleSpecialType(
FD, FieldTy, KernelArgDescription(KernelArgDescription::Sampler));
}

bool handlePointerType(FieldDecl *FD, QualType FieldTy) final {
KernelArgDescription Desc = KernelArgDescription::None;
ParmVarDecl *KernelParameter = DC.getParamVarDeclsForCurrentField()[0];
// Compiler generated openCL kernel argument for current pointer field
// is not a pointer. This means we are processing a nested pointer and
// the openCL kernel argument is of type __wrapper_class.
if (!KernelParameter->getType()->isPointerType())
Desc = KernelArgDescription::WrappedPointer;
return handleSpecialType(FD, FieldTy, Desc);
}

bool handleScalarType(FieldDecl *FD, QualType FieldTy) final {
addParam(FD, FieldTy);
return true;
}

bool handleSimpleArrayType(FieldDecl *FD, QualType FieldTy) final {
// Simple arrays are always wrapped.
handleSpecialType(FD, FieldTy,
KernelArgDescription(KernelArgDescription::WrappedArray));
return true;
}

bool handleNonDecompStruct(const CXXRecordDecl *, FieldDecl *FD,
QualType Ty) final {
addParam(FD, Ty);
return true;
}

bool handleNonDecompStruct(const CXXRecordDecl *Base,
const CXXBaseSpecifier &BS, QualType Ty) final {
addParam(Ty, KernelArgDescription(KernelArgDescription::BaseClass));
return true;
}

bool handleUnionType(FieldDecl *FD, QualType FieldTy) final {
return handleScalarType(FD, FieldTy);
}

bool handleSyclHalfType(FieldDecl *FD, QualType FieldTy) final {
addParam(FD, FieldTy);
return true;
}

bool handleSyclStreamType(FieldDecl *FD, QualType FieldTy) final {
// For the current implementation of stream class, the Visitor 'handles'
// stream argument and then visits each accessor field in stream. Therefore
// handleSpecialType in this case only adds a single argument for stream.
// The arguments corresponding to accessors in stream are handled in
// handleSyclAccessorType. The opt-report therefore does not diffrentiate
// between the accessors in streams and accessors captured by SYCL kernel.
// Once stream API is modified to use __init(), the visitor will no longer
// visit the stream object and opt-report output for stream class will be
// similar to that of other special types.
return handleSpecialType(
FD, FieldTy, KernelArgDescription(KernelArgDescription::Stream));
}

void handleSyclKernelHandlerType() {
ASTContext &Context = SemaRef.getASTContext();
if (isDefaultSPIRArch(Context))
return;
addParam(DC.getParamVarDeclsForCurrentField()[0]->getType(),
KernelArgDescription(KernelArgDescription::KernelHandler));
}
using SyclKernelFieldHandler::handleSyclHalfType;
using SyclKernelFieldHandler::handleSyclSamplerType;
using SyclKernelFieldHandler::handleSyclStreamType;
};

static CXXMethodDecl *getOperatorParens(const CXXRecordDecl *Rec) {
for (auto *MD : Rec->methods()) {
if (MD->getOverloadedOperator() == OO_Call)
Expand Down Expand Up @@ -3552,18 +3734,20 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc,
StableName, KernelCallerFunc);

SyclKernelIntFooterCreator int_footer(*this, getSyclIntegrationFooter());
SyclOptReportCreator opt_report(*this, kernel_decl, KernelObj->getLocation());

KernelObjVisitor Visitor{*this};
Visitor.VisitRecordBases(KernelObj, kernel_decl, kernel_body, int_header,
int_footer);
int_footer, opt_report);
Visitor.VisitRecordFields(KernelObj, kernel_decl, kernel_body, int_header,
int_footer);
int_footer, opt_report);

if (ParmVarDecl *KernelHandlerArg =
getSyclKernelHandlerArg(KernelCallerFunc)) {
kernel_decl.handleSyclKernelHandlerType();
kernel_body.handleSyclKernelHandlerType(KernelHandlerArg);
int_header.handleSyclKernelHandlerType(KernelHandlerArg->getType());
opt_report.handleSyclKernelHandlerType();
}
}

Expand Down
Loading