Skip to content

Commit 36f5f6f

Browse files
committed
merge upstream from Microsoft
2 parents 7c15568 + 50764ba commit 36f5f6f

51 files changed

Lines changed: 1279 additions & 488 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tools/clang/include/clang/Basic/Attr.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,18 @@ def HLSLCXXOverload : InheritableAttr {
945945
let Documentation = [Undocumented];
946946
}
947947

948+
// This attribute means something subtly different from the HLSLIntrinsic
949+
// attribute. Prior to this being introduced a subset of the HLSL intrinsics
950+
// were placed in the `hlsl` namespace, but not in a way that actually qualified
951+
// their name lookup. The presence of the namespace as the declcontext was then
952+
// sused in CGExpr to determine how to emit the call. This attribute replaces
953+
// the namespace in that context.
954+
def HLSLBuiltinCall : InheritableAttr {
955+
let Spellings = [];
956+
let Subjects = SubjectList<[Function]>;
957+
let Documentation = [Undocumented];
958+
}
959+
948960
def HLSLVector : InheritableAttr {
949961
let Spellings = []; // No spellings!
950962
let Subjects = SubjectList<[CXXRecord]>;

tools/clang/include/clang/SPIRV/SpirvBuilder.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,23 @@ class SpirvBuilder {
678678
bool isPrecise, bool isNointerp, llvm::StringRef name = "",
679679
llvm::Optional<SpirvInstruction *> init = llvm::None,
680680
SourceLocation loc = {});
681+
682+
// Adds a variable to the module.
681683
SpirvVariable *
682684
addModuleVar(const SpirvType *valueType, spv::StorageClass storageClass,
683685
bool isPrecise, bool isNointerp, llvm::StringRef name = "",
684686
llvm::Optional<SpirvInstruction *> init = llvm::None,
685687
SourceLocation loc = {});
686688

689+
// Adds a variable to the module. It will be placed in the variable list
690+
// before `pos`.
691+
SpirvVariable *
692+
addModuleVar(const SpirvType *valueType, spv::StorageClass storageClass,
693+
bool isPrecise, bool isNointerp, SpirvInstruction *before,
694+
llvm::StringRef name = "",
695+
llvm::Optional<SpirvInstruction *> init = llvm::None,
696+
SourceLocation loc = {});
697+
687698
/// \brief Decorates the given target with the given location.
688699
void decorateLocation(SpirvInstruction *target, uint32_t location);
689700

tools/clang/include/clang/SPIRV/SpirvModule.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ class SpirvModule {
139139
// Adds a variable to the module.
140140
void addVariable(SpirvVariable *);
141141

142+
// Adds a variable to the module immediately before `pos`.
143+
// If `pos` is not found, `var` is added at the end of the variable list.
144+
void addVariable(SpirvVariable *var, SpirvInstruction *pos);
145+
142146
// Adds a decoration to the module.
143147
void addDecoration(SpirvDecoration *);
144148

@@ -161,6 +165,10 @@ class SpirvModule {
161165
return debugInstructions;
162166
}
163167

168+
// Access the one DebugCompilationUnit per module
169+
SpirvDebugCompilationUnit *getDebugCompilationUnit();
170+
void setDebugCompilationUnit(SpirvDebugCompilationUnit *unit);
171+
164172
// Adds the given OpModuleProcessed to the module.
165173
void addModuleProcessed(SpirvModuleProcessed *);
166174

@@ -220,6 +228,10 @@ class SpirvModule {
220228

221229
// Keep all rich DebugInfo instructions.
222230
llvm::SmallVector<SpirvDebugInstruction *, 32> debugInstructions;
231+
232+
// There is one debugCompilationUnit per module
233+
SpirvDebugCompilationUnit *debugCompilationUnit;
234+
223235
// Whether current module is in pervertex interpolation mode.
224236
bool perVertexInterp;
225237
};

tools/clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,14 +3646,8 @@ RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
36463646
if (unsigned builtinID = FD->getBuiltinID())
36473647
return EmitBuiltinExpr(FD, builtinID, E, ReturnValue);
36483648
// HLSL Change Starts
3649-
if (getLangOpts().HLSL) {
3650-
if (const NamespaceDecl *ns = dyn_cast<NamespaceDecl>(FD->getParent())) {
3651-
if (ns->getName() == "hlsl") {
3652-
// do hlsl intrinsic generation
3653-
return EmitHLSLBuiltinCallExpr(FD, E, ReturnValue);
3654-
}
3655-
}
3656-
}
3649+
if (getLangOpts().HLSL && FD->hasAttr<HLSLBuiltinCallAttr>())
3650+
return EmitHLSLBuiltinCallExpr(FD, E, ReturnValue);
36573651
// HLSL Change End
36583652
}
36593653

tools/clang/lib/SPIRV/AstTypeProbe.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,10 @@ void forEachSpirvField(
16011601
uint32_t lastConvertedIndex = 0;
16021602
size_t astFieldIndex = 0;
16031603
for (const auto &base : cxxDecl->bases()) {
1604-
const auto &type = base.getType();
1604+
auto type = base.getType();
1605+
if (auto *templatedType = dyn_cast<SubstTemplateTypeParmType>(type))
1606+
type = templatedType->getReplacementType();
1607+
16051608
const auto &spirvField = spirvType->getFields()[astFieldIndex];
16061609
if (!operation(spirvField.fieldIndex, type, spirvField)) {
16071610
return;
@@ -1620,7 +1623,10 @@ void forEachSpirvField(
16201623
continue;
16211624
}
16221625

1623-
const auto &type = field->getType();
1626+
auto type = field->getType();
1627+
if (auto *templatedType = dyn_cast<SubstTemplateTypeParmType>(type))
1628+
type = templatedType->getReplacementType();
1629+
16241630
if (!operation(currentFieldIndex, type, spirvField)) {
16251631
return;
16261632
}

tools/clang/lib/SPIRV/DebugTypeVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SpirvDebugTypeComposite *DebugTypeVisitor::createDebugTypeComposite(
6262
} else {
6363
auto *dbgSrc = spvBuilder.createDebugSource(file);
6464
setDefaultDebugInfo(dbgSrc);
65-
auto dbgCompUnit = spvBuilder.createDebugCompilationUnit(dbgSrc);
65+
auto dbgCompUnit = spvBuilder.getModule()->getDebugCompilationUnit();
6666
setDefaultDebugInfo(dbgCompUnit);
6767
debugInfo =
6868
&debugInfoMap.insert({file, RichDebugInfo(dbgSrc, dbgCompUnit)})

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ void DeclResultIdMapper::createCounterVar(
18611861
}
18621862

18631863
SpirvVariable *counterInstr = spvBuilder.addModuleVar(
1864-
counterType, sc, /*isPrecise*/ false, false, counterName);
1864+
counterType, sc, /*isPrecise*/ false, false, declInstr, counterName);
18651865

18661866
if (!isAlias) {
18671867
// Non-alias counter variables should be put in to resourceVars so that

tools/clang/lib/SPIRV/EmitVisitor.cpp

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ uint32_t getHeaderVersion(spv_target_env env) {
136136
std::string
137137
ReadSourceCode(llvm::StringRef filePath,
138138
const clang::spirv::SpirvCodeGenOptions &spvOptions) {
139+
140+
std::string localFilePath(filePath.begin(), filePath.end());
139141
try {
140142
dxc::DxcDllSupport dllSupport;
141143
IFT(dllSupport.Initialize());
@@ -154,7 +156,10 @@ ReadSourceCode(llvm::StringRef filePath,
154156
} catch (...) {
155157
// An exception has occurred while reading the file
156158
// return the original source (which may have been supplied directly)
157-
if (!spvOptions.origSource.empty()) {
159+
// only for the main input file
160+
if ((!strcmp(localFilePath.c_str(), "hlsl.hlsl") &&
161+
spvOptions.inputFile.empty()) ||
162+
!strcmp(localFilePath.c_str(), spvOptions.inputFile.c_str())) {
158163
return spvOptions.origSource.c_str();
159164
}
160165
return "";
@@ -2639,80 +2644,15 @@ uint32_t EmitTypeHandler::emitType(const SpirvType *type) {
26392644
// NodePayloadArray types
26402645
else if (const auto *npaType = dyn_cast<NodePayloadArrayType>(type)) {
26412646
const uint32_t elemTypeId = emitType(npaType->getElementType());
2647+
2648+
// Output the decorations for the type first. This will create other values
2649+
// that are on the decorations, and they must appear before the type.
2650+
emitDecorationsForNodePayloadArrayTypes(npaType, id);
2651+
26422652
initTypeInstruction(spv::Op::OpTypeNodePayloadArrayAMDX);
26432653
curTypeInst.push_back(id);
26442654
curTypeInst.push_back(elemTypeId);
26452655
finalizeTypeInstruction();
2646-
2647-
// Emit decorations
2648-
const ParmVarDecl *nodeDecl = npaType->getNodeDecl();
2649-
if (hlsl::IsHLSLNodeOutputType(nodeDecl->getType())) {
2650-
StringRef name = nodeDecl->getName();
2651-
unsigned index = 0;
2652-
if (auto nodeID = nodeDecl->getAttr<HLSLNodeIdAttr>()) {
2653-
name = nodeID->getName();
2654-
index = nodeID->getArrayIndex();
2655-
}
2656-
2657-
auto *str = new (context) SpirvConstantString(name);
2658-
uint32_t nodeName = getOrCreateConstantString(str);
2659-
emitDecoration(id, spv::Decoration::PayloadNodeNameAMDX, {nodeName},
2660-
llvm::None, true);
2661-
if (index) {
2662-
uint32_t baseIndex = getOrCreateConstantInt(
2663-
llvm::APInt(32, index), context.getUIntType(32), false);
2664-
emitDecoration(id, spv::Decoration::PayloadNodeBaseIndexAMDX,
2665-
{baseIndex}, llvm::None, true);
2666-
}
2667-
}
2668-
2669-
uint32_t maxRecords;
2670-
if (const auto *attr = nodeDecl->getAttr<HLSLMaxRecordsAttr>()) {
2671-
maxRecords = getOrCreateConstantInt(llvm::APInt(32, attr->getMaxCount()),
2672-
context.getUIntType(32), false);
2673-
} else {
2674-
maxRecords = getOrCreateConstantInt(llvm::APInt(32, 1),
2675-
context.getUIntType(32), false);
2676-
}
2677-
emitDecoration(id, spv::Decoration::NodeMaxPayloadsAMDX, {maxRecords},
2678-
llvm::None, true);
2679-
2680-
if (const auto *attr = nodeDecl->getAttr<HLSLMaxRecordsSharedWithAttr>()) {
2681-
const DeclContext *dc = nodeDecl->getParentFunctionOrMethod();
2682-
if (const auto *funDecl = dyn_cast_or_null<FunctionDecl>(dc)) {
2683-
IdentifierInfo *ii = attr->getName();
2684-
bool alreadyExists = false;
2685-
for (auto *paramDecl : funDecl->params()) {
2686-
if (paramDecl->getIdentifier() == ii) {
2687-
assert(paramDecl != nodeDecl);
2688-
auto otherType = context.getNodeDeclPayloadType(paramDecl);
2689-
const uint32_t otherId =
2690-
getResultIdForType(otherType, &alreadyExists);
2691-
assert(alreadyExists && "forward references not allowed in "
2692-
"MaxRecordsSharedWith attribute");
2693-
emitDecoration(id, spv::Decoration::NodeSharesPayloadLimitsWithAMDX,
2694-
{otherId}, llvm::None, true);
2695-
break;
2696-
}
2697-
}
2698-
assert(alreadyExists &&
2699-
"invalid reference in MaxRecordsSharedWith attribute");
2700-
}
2701-
}
2702-
if (const auto *attr = nodeDecl->getAttr<HLSLAllowSparseNodesAttr>()) {
2703-
emitDecoration(id, spv::Decoration::PayloadNodeSparseArrayAMDX, {},
2704-
llvm::None);
2705-
}
2706-
if (const auto *attr = nodeDecl->getAttr<HLSLUnboundedSparseNodesAttr>()) {
2707-
emitDecoration(id, spv::Decoration::PayloadNodeSparseArrayAMDX, {},
2708-
llvm::None);
2709-
}
2710-
if (const auto *attr = nodeDecl->getAttr<HLSLNodeArraySizeAttr>()) {
2711-
uint32_t arraySize = getOrCreateConstantInt(
2712-
llvm::APInt(32, attr->getCount()), context.getUIntType(32), false);
2713-
emitDecoration(id, spv::Decoration::PayloadNodeArraySizeAMDX, {arraySize},
2714-
llvm::None, true);
2715-
}
27162656
}
27172657
// Structure types
27182658
else if (const auto *structType = dyn_cast<StructType>(type)) {
@@ -2993,5 +2933,78 @@ void EmitTypeHandler::emitNameForType(llvm::StringRef name,
29932933
nameInstr.end());
29942934
}
29952935

2936+
void EmitTypeHandler::emitDecorationsForNodePayloadArrayTypes(
2937+
const NodePayloadArrayType *npaType, uint32_t id) {
2938+
// Emit decorations
2939+
const ParmVarDecl *nodeDecl = npaType->getNodeDecl();
2940+
if (hlsl::IsHLSLNodeOutputType(nodeDecl->getType())) {
2941+
StringRef name = nodeDecl->getName();
2942+
unsigned index = 0;
2943+
if (auto nodeID = nodeDecl->getAttr<HLSLNodeIdAttr>()) {
2944+
name = nodeID->getName();
2945+
index = nodeID->getArrayIndex();
2946+
}
2947+
2948+
auto *str = new (context) SpirvConstantString(name);
2949+
uint32_t nodeName = getOrCreateConstantString(str);
2950+
emitDecoration(id, spv::Decoration::PayloadNodeNameAMDX, {nodeName},
2951+
llvm::None, true);
2952+
if (index) {
2953+
uint32_t baseIndex = getOrCreateConstantInt(
2954+
llvm::APInt(32, index), context.getUIntType(32), false);
2955+
emitDecoration(id, spv::Decoration::PayloadNodeBaseIndexAMDX, {baseIndex},
2956+
llvm::None, true);
2957+
}
2958+
}
2959+
2960+
uint32_t maxRecords;
2961+
if (const auto *attr = nodeDecl->getAttr<HLSLMaxRecordsAttr>()) {
2962+
maxRecords = getOrCreateConstantInt(llvm::APInt(32, attr->getMaxCount()),
2963+
context.getUIntType(32), false);
2964+
} else {
2965+
maxRecords = getOrCreateConstantInt(llvm::APInt(32, 1),
2966+
context.getUIntType(32), false);
2967+
}
2968+
emitDecoration(id, spv::Decoration::NodeMaxPayloadsAMDX, {maxRecords},
2969+
llvm::None, true);
2970+
2971+
if (const auto *attr = nodeDecl->getAttr<HLSLMaxRecordsSharedWithAttr>()) {
2972+
const DeclContext *dc = nodeDecl->getParentFunctionOrMethod();
2973+
if (const auto *funDecl = dyn_cast_or_null<FunctionDecl>(dc)) {
2974+
IdentifierInfo *ii = attr->getName();
2975+
bool alreadyExists = false;
2976+
for (auto *paramDecl : funDecl->params()) {
2977+
if (paramDecl->getIdentifier() == ii) {
2978+
assert(paramDecl != nodeDecl);
2979+
auto otherType = context.getNodeDeclPayloadType(paramDecl);
2980+
const uint32_t otherId =
2981+
getResultIdForType(otherType, &alreadyExists);
2982+
assert(alreadyExists && "forward references not allowed in "
2983+
"MaxRecordsSharedWith attribute");
2984+
emitDecoration(id, spv::Decoration::NodeSharesPayloadLimitsWithAMDX,
2985+
{otherId}, llvm::None, true);
2986+
break;
2987+
}
2988+
}
2989+
assert(alreadyExists &&
2990+
"invalid reference in MaxRecordsSharedWith attribute");
2991+
}
2992+
}
2993+
if (const auto *attr = nodeDecl->getAttr<HLSLAllowSparseNodesAttr>()) {
2994+
emitDecoration(id, spv::Decoration::PayloadNodeSparseArrayAMDX, {},
2995+
llvm::None);
2996+
}
2997+
if (const auto *attr = nodeDecl->getAttr<HLSLUnboundedSparseNodesAttr>()) {
2998+
emitDecoration(id, spv::Decoration::PayloadNodeSparseArrayAMDX, {},
2999+
llvm::None);
3000+
}
3001+
if (const auto *attr = nodeDecl->getAttr<HLSLNodeArraySizeAttr>()) {
3002+
uint32_t arraySize = getOrCreateConstantInt(
3003+
llvm::APInt(32, attr->getCount()), context.getUIntType(32), false);
3004+
emitDecoration(id, spv::Decoration::PayloadNodeArraySizeAMDX, {arraySize},
3005+
llvm::None, true);
3006+
}
3007+
}
3008+
29963009
} // end namespace spirv
29973010
} // end namespace clang

tools/clang/lib/SPIRV/EmitVisitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ class EmitTypeHandler {
135135
void emitNameForType(llvm::StringRef name, uint32_t targetTypeId,
136136
llvm::Optional<uint32_t> memberIndex = llvm::None);
137137

138+
void
139+
emitDecorationsForNodePayloadArrayTypes(const NodePayloadArrayType *npaType,
140+
uint32_t id);
141+
138142
// There is no guarantee that an instruction or a function or a basic block
139143
// has been assigned result-id. This method returns the result-id for the
140144
// given object. If a result-id has not been assigned yet, it'll assign

0 commit comments

Comments
 (0)