Skip to content
50 changes: 27 additions & 23 deletions tools/clang/lib/Sema/SemaHLSLDiagnoseTU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ std::vector<FunctionDecl *> GetAllExportedFDecls(clang::Sema *self) {
}

void GatherGlobalsWithInitializers(
DeclContext *DC, llvm::SmallVectorImpl<VarDecl *> &GlobalsWithInit) {
DeclContext *DC, llvm::SmallVectorImpl<VarDecl *> &GlobalsWithInit,
llvm::SmallVectorImpl<VarDecl *> &SubObjects) {
for (auto *D : DC->decls()) {
// Skip built-ins and function decls.
if (D->isImplicit() || isa<FunctionDecl>(D))
Expand All @@ -310,11 +311,19 @@ void GatherGlobalsWithInitializers(
// Add if user-defined static or groupshared global with initializer.
if (VD->hasInit() && VD->hasGlobalStorage() &&
(VD->getStorageClass() == SC_Static ||
VD->hasAttr<HLSLGroupSharedAttr>()))
GlobalsWithInit.push_back(VD);
VD->hasAttr<HLSLGroupSharedAttr>())) {
// Place subobjects in a separate collection.
QualType QT = VD->getType();
if (const RecordType *RT = QT->getAs<RecordType>()) {
RecordDecl *RD = RT->getDecl();
if (RD->hasAttr<HLSLSubObjectAttr>())
SubObjects.push_back(VD);
Comment thread
tex3d marked this conversation as resolved.
} else
GlobalsWithInit.push_back(VD);
}
} else if (auto *DC = dyn_cast<DeclContext>(D)) {
// Recurse into DeclContexts like namespace, cbuffer, class/struct, etc.
GatherGlobalsWithInitializers(DC, GlobalsWithInit);
GatherGlobalsWithInitializers(DC, GlobalsWithInit, SubObjects);
}
}
}
Expand Down Expand Up @@ -592,14 +601,25 @@ void hlsl::DiagnoseTranslationUnit(clang::Sema *self) {
hlsl::ShaderModel::GetByName(self->getLangOpts().HLSLProfile.c_str());

llvm::SmallVector<VarDecl *, 16> GlobalsWithInit;
GatherGlobalsWithInitializers(self->getASTContext().getTranslationUnitDecl(),
GlobalsWithInit);

llvm::SmallVector<VarDecl *, 16> SubObjects;
std::set<FunctionDecl *> DiagnosedRecursiveDecls;
llvm::SmallPtrSet<CallExpr *, 16> DiagnosedCalls;
llvm::SmallPtrSet<DeclRefExpr *, 16> DeclAvailabilityChecked;
llvm::SmallSet<SourceLocation, 16> DiagnosedTypeLocs;

GatherGlobalsWithInitializers(self->getASTContext().getTranslationUnitDecl(),
GlobalsWithInit, SubObjects);

if (shaderModel->GetKind() == DXIL::ShaderKind::Library) {
for (VarDecl *VD : SubObjects) {
DXIL::NodeLaunchType NodeLaunchTy = DXIL::NodeLaunchType::Invalid;
HLSLReachableDiagnoseVisitor Visitor(
self, shaderModel, shaderModel->GetKind(), NodeLaunchTy, nullptr,
DiagnosedCalls, DeclAvailabilityChecked, DiagnosedTypeLocs);
Visitor.TraverseDecl(VD);
}
}
Comment thread
tex3d marked this conversation as resolved.

// for each FDecl, check for recursion
for (FunctionDecl *FDecl : FDeclsToCheck) {
CallGraphWithRecurseGuard callGraph;
Expand Down Expand Up @@ -732,20 +752,4 @@ void hlsl::DiagnoseTranslationUnit(clang::Sema *self) {
for (FunctionDecl *FD : callGraph.GetVisitedFunctions())
Visitor.TraverseDecl(FD);
}

if (shaderModel->GetKind() == DXIL::ShaderKind::Library) {
for (VarDecl *VD : GlobalsWithInit) {
DXIL::NodeLaunchType NodeLaunchTy = DXIL::NodeLaunchType::Invalid;
HLSLReachableDiagnoseVisitor Visitor(
self, shaderModel, shaderModel->GetKind(), NodeLaunchTy, nullptr,
DiagnosedCalls, DeclAvailabilityChecked, DiagnosedTypeLocs);
QualType QT = VD->getType();
if (const RecordType *RT = QT->getAs<RecordType>()) {
RecordDecl *RD = RT->getDecl();
if (!RD->hasAttr<HLSLSubObjectAttr>())
continue;
Visitor.TraverseDecl(VD);
}
}
}
}
Loading