Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions include/dxc/Support/HLSLOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,6 @@ def rw_decl_global_cb : Flag<["-", "/"], "decl-global-cb">, Group<hlslrewrite_Gr
HelpText<"Collect all global constants outside cbuffer declarations into cbuffer GlobalCB { ... }. Still experimental, not all dependency scenarios handled.">;
// Also removed: compress, decompress, /Gch (child effect), /Gpp (partial precision)
// /Op - no support for preshaders.

def fvk_disable_hlsl_intrinsics : Flag<["-"], "fvk-disable-hlsl-intrinsics">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
HelpText<"Disable HLSL intrinsics">;
1 change: 1 addition & 0 deletions include/dxc/Support/SPIRVOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class SpirvLayoutRule {
};

struct SpirvCodeGenOptions {
bool disableHLSLIntrinsics;
/// Disable legalization and optimization and emit raw SPIR-V
bool codeGenHighLevel;
bool debugInfoFile;
Expand Down
2 changes: 2 additions & 0 deletions lib/DxcSupport/HLSLOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,

// SPIRV Change Starts
#ifdef ENABLE_SPIRV_CODEGEN
opts.SpirvOptions.disableHLSLIntrinsics =
Args.hasFlag(OPT_fvk_disable_hlsl_intrinsics, OPT_INVALID, false);
opts.GenSPIRV = Args.hasFlag(OPT_spirv, OPT_INVALID, false);
opts.SpirvOptions.invertY =
Args.hasFlag(OPT_fvk_invert_y, OPT_INVALID, false);
Expand Down
2 changes: 1 addition & 1 deletion tools/clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// It is normally invoked after ASTContext construction.
///
/// \param Target The target
void InitBuiltinTypes(const TargetInfo &Target);
void InitBuiltinTypes(const TargetInfo &Target, bool ignoreHLSLIntrinsics);

private:
void InitBuiltinType(CanQualType &R, BuiltinType::Kind K);
Expand Down
3 changes: 2 additions & 1 deletion tools/clang/include/clang/AST/HlslTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace hlsl {

/// <summary>Initializes the specified context to support HLSL
/// compilation.</summary>
void InitializeASTContextForHLSL(clang::ASTContext &context);
void InitializeASTContextForHLSL(clang::ASTContext &context,
bool ignoreHLSLIntrinsics);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Type system enumerations.
Expand Down
2 changes: 1 addition & 1 deletion tools/clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ class CompilerInstance : public ModuleLoader {
std::string getSpecificModuleCachePath();

/// Create the AST context.
void createASTContext();
void createASTContext(bool ignoreHLSLIntrinsics = false);

/// Create an external AST source to read a PCH file and attach it to the AST
/// context.
Expand Down
3 changes: 2 additions & 1 deletion tools/clang/include/clang/Frontend/FrontendAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ class FrontendAction {
///
/// \return True on success; on failure the compilation of this file should
/// be aborted and neither Execute() nor EndSourceFile() should be called.
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input);
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input,
bool ignoreHLSLIntrinsics = false);

/// \brief Set the source manager's main input file, and run the action.
bool Execute();
Expand Down
4 changes: 2 additions & 2 deletions tools/clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
Types.push_back(Ty);
}

void ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
void ASTContext::InitBuiltinTypes(const TargetInfo &Target, bool ignoreHLSLIntrinsics) {
assert((!this->Target || this->Target == &Target) &&
"Incorrect target reinitialization");
assert(VoidTy.isNull() && "Context reinitialized?");
Expand Down Expand Up @@ -1108,7 +1108,7 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target) {

HLSLStringTy = this->getPointerType(CharTy);

hlsl::InitializeASTContextForHLSL(*this); // Previously in constructor, guarded by !DelayInitialization
hlsl::InitializeASTContextForHLSL(*this, ignoreHLSLIntrinsics); // Previously in constructor, guarded by !DelayInitialization
}
// HLSL Change Ends
}
Expand Down
4 changes: 2 additions & 2 deletions tools/clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ std::string CompilerInstance::getSpecificModuleCachePath() {

// ASTContext

void CompilerInstance::createASTContext() {
void CompilerInstance::createASTContext(bool ignoreHLSLIntrinsics) {
Preprocessor &PP = getPreprocessor();
Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
PP.getIdentifierTable(), PP.getSelectorTable(),
PP.getBuiltinInfo());
Context->InitBuiltinTypes(getTarget());
Context->InitBuiltinTypes(getTarget(), ignoreHLSLIntrinsics);
}

// ExternalASTSource
Expand Down
5 changes: 3 additions & 2 deletions tools/clang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
}

bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
const FrontendInputFile &Input) {
const FrontendInputFile &Input,
bool ignoreHLSLIntrinsics) {
assert(!Instance && "Already processing a source file!");
assert(!Input.isEmpty() && "Unexpected empty filename!");
setCurrentInput(Input);
Expand Down Expand Up @@ -323,7 +324,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
if (!usesPreprocessorOnly()) {
// Parsing a model file should reuse the existing ASTContext.
if (!isModelParsingAction())
CI.createASTContext();
CI.createASTContext(ignoreHLSLIntrinsics);

std::unique_ptr<ASTConsumer> Consumer =
CreateWrappedASTConsumer(CI, InputFile);
Expand Down
118 changes: 114 additions & 4 deletions tools/clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,9 @@ static TypedefDecl *CreateGlobalTypedef(ASTContext *context, const char *ident,

class HLSLExternalSource : public ExternalSemaSource {
private:

const bool m_disableHLSLIntrinsics;

// Inner types.
struct FindStructBasicTypeResult {
ArBasicKind Kind; // Kind of struct (eg, AR_OBJECT_TEXTURE2D)
Expand Down Expand Up @@ -4113,13 +4116,14 @@ class HLSLExternalSource : public ExternalSemaSource {
}

public:
HLSLExternalSource()
HLSLExternalSource(bool disableHLSLIntrinsics)
: m_matrixTemplateDecl(nullptr), m_vectorTemplateDecl(nullptr),
m_vkIntegralConstantTemplateDecl(nullptr),
m_vkLiteralTemplateDecl(nullptr),
m_vkBufferPointerTemplateDecl(nullptr), m_hlslNSDecl(nullptr),
m_vkNSDecl(nullptr), m_dxNSDecl(nullptr), m_context(nullptr),
m_sema(nullptr), m_hlslStringTypedef(nullptr) {
m_sema(nullptr), m_hlslStringTypedef(nullptr),
m_disableHLSLIntrinsics(disableHLSLIntrinsics) {
memset(m_matrixTypes, 0, sizeof(m_matrixTypes));
memset(m_matrixShorthandTypes, 0, sizeof(m_matrixShorthandTypes));
memset(m_vectorTypes, 0, sizeof(m_vectorTypes));
Expand Down Expand Up @@ -5131,12 +5135,118 @@ class HLSLExternalSource : public ExternalSemaSource {
bool IsValidObjectElement(LPCSTR tableName, IntrinsicOp op,
QualType objectElement);

bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier)
{
if (!m_disableHLSLIntrinsics)
return true;

static const std::unordered_set<std::string> allowedHLSLIntrinsics = {
"Abort",
"AcceptHitAndEndSearch",
"AllocateRayQuery",
"CallShader",
"CommitNonOpaqueTriangleHit",
"CommitProceduralPrimitiveHit",
"CommittedGeometryIndex",
"CommittedInstanceContributionToHitGroupIndex",
"CommittedInstanceID",
"CommittedInstanceIndex",
"CommittedObjectRayDirection",
"CommittedObjectRayOrigin",
"CommittedObjectToWorld3x4",
"CommittedObjectToWorld4x3",
"CommittedPrimitiveIndex",
"CommittedRayT",
"CommittedStatus",
"CommittedTriangleBarycentrics",
"CommittedTriangleFrontFace",
"CommittedWorldToObject3x4",
"CommittedWorldToObject4x3",
"CandidateGeometryIndex",
"CandidateInstanceContributionToHitGroupIndex",
"CandidateInstanceID",
"CandidateInstanceIndex",
"CandidateObjectRayDirection",
"CandidateObjectRayOrigin",
"CandidateObjectToWorld3x4",
"CandidateObjectToWorld4x3",
"CandidatePrimitiveIndex",
"CandidateProceduralPrimitiveNonOpaque",
"CandidateTriangleBarycentrics",
"CandidateTriangleFrontFace",
"CandidateTriangleRayT",
"CandidateType",
"CandidateWorldToObject3x4",
"CandidateWorldToObject4x3",
"DispatchRaysDimensions",
"DispatchRaysIndex",
"FromRayQuery",
"GeometryIndex",
"GetGeometryIndex",
"GetHitKind",
"GetInstanceID",
"GetInstanceIndex",
"GetObjectRayDirection",
"GetObjectRayOrigin",
"GetObjectToWorld3x4",
"GetObjectToWorld4x3",
"GetPrimitiveIndex",
"GetRayFlags",
"GetRayTCurrent",
"GetRayTMin",
"GetShaderTableIndex",
"GetWorldRayDirection",
"GetWorldRayOrigin",
"GetWorldToObject3x4",
"GetWorldToObject4x3",
"HitKind",
"IgnoreHit",
"InstanceID",
"InstanceIndex",
"Invoke",
"IsHit",
"IsMiss",
"IsNop",
"LoadLocalRootTableConstant",
"MakeMiss",
"MakeNop",
"MaybeReorderThread",
"ObjectRayDirection",
"ObjectRayOrigin",
"ObjectToWorld",
"ObjectToWorld3x4",
"ObjectToWorld4x3",
"PrimitiveIndex",
"Proceed",
"RayFlags",
"RayTCurrent",
"RayTMin",
"ReportHit",
"TraceRay",
"TraceRayInline",
"WorldRayDirection",
"WorldRayOrigin"
};

auto it = allowedHLSLIntrinsics.find(std::string(intrinsicNameIdentifier));
return it != allowedHLSLIntrinsics.end();
}

// Returns the iterator with the first entry that matches the requirement
IntrinsicDefIter FindIntrinsicByNameAndArgCount(const HLSL_INTRINSIC *table,
size_t tableSize,
StringRef typeName,
StringRef nameIdentifier,
size_t argumentCount) {
// TODO: only check if the flag "devsh-disable-hlsl-intrinsics" is enabled
if (!checkIfIntrinsicIsAllowed(nameIdentifier))
{
return IntrinsicDefIter::CreateStart(
table, tableSize, table + tableSize,
IntrinsicTableDefIter::CreateStart(m_intrinsicTables, typeName,
nameIdentifier, argumentCount));
}

// This is implemented by a linear scan for now.
// We tested binary search on tables, and there was no performance gain on
// samples probably for the following reasons.
Expand Down Expand Up @@ -13452,8 +13562,8 @@ hlsl::TrySubscriptIndexInitialization(clang::Sema *self, clang::Expr *SrcExpr,

/// <summary>Performs HLSL-specific initialization on the specified
/// context.</summary>
void hlsl::InitializeASTContextForHLSL(ASTContext &context) {
HLSLExternalSource *hlslSource = new HLSLExternalSource();
void hlsl::InitializeASTContextForHLSL(ASTContext &context, bool ignoreHLSLIntrinsics) {
HLSLExternalSource *hlslSource = new HLSLExternalSource(ignoreHLSLIntrinsics);
IntrusiveRefCntPtr<ExternalASTSource> externalSource(hlslSource);
if (hlslSource->Initialize(context)) {
context.setExternalSource(externalSource);
Expand Down
5 changes: 4 additions & 1 deletion tools/clang/tools/dxcompiler/dxcompilerobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// //
///////////////////////////////////////////////////////////////////////////////

#include "clang/Sema/SemaHLSL.h"

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
Expand Down Expand Up @@ -964,7 +966,8 @@ class DxcCompiler : public IDxcCompiler3,
compiler.getCodeGenOpts().SpirvOptions = opts.SpirvOptions;
clang::EmitSpirvAction action;
FrontendInputFile file(pUtf8SourceName, IK_HLSL);
action.BeginSourceFile(compiler, file);
action.BeginSourceFile(
compiler, file, opts.SpirvOptions.disableHLSLIntrinsics);
action.Execute();
action.EndSourceFile();
outStream.flush();
Expand Down
Loading