Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 devsh_disable_hlsl_intrinsics : Flag<["-"], "devsh-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 devshDisableHLSLIntrinsics;
/// 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.devshDisableHLSLIntrinsics =
Args.hasFlag(OPT_devsh_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
13 changes: 13 additions & 0 deletions tools/clang/include/clang/Sema/SemaHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"

#include <optional>
extern std::optional<bool> disableHLSLIntrinsicsGlobalVariableBecauseIDontCare;

inline bool shouldDisableHLSLIntrinsics()
{
assert(disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value());

if (!disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value())
exit(-1);

return disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.value();
}

// Forward declarations.
struct IDxcIntrinsicTable;
namespace clang {
Expand Down
108 changes: 108 additions & 0 deletions tools/clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <bitset>
#include <float.h>

std::optional<bool> disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = std::nullopt;

enum ArBasicKind {
AR_BASIC_BOOL,
AR_BASIC_LITERAL_FLOAT,
Expand Down Expand Up @@ -5131,12 +5133,118 @@ class HLSLExternalSource : public ExternalSemaSource {
bool IsValidObjectElement(LPCSTR tableName, IntrinsicOp op,
QualType objectElement);

static bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier)
{
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",
"WorldToObject",
"WorldToObject3x4",
"WorldToObject4x3"
};

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 (shouldDisableHLSLIntrinsics() && !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
4 changes: 4 additions & 0 deletions 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 @@ -626,6 +628,8 @@ class DxcCompiler : public IDxcCompiler3,
static_cast<const char *>(pOrigUtf8Source->GetStringPointer()),
pOrigUtf8Source->GetStringLength());

disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = opts.SpirvOptions.devshDisableHLSLIntrinsics;

CComPtr<IDxcResult> pSrcCodeResult;
std::vector<LPCWSTR> PreprocessArgs;
PreprocessArgs.reserve(argCount + 1);
Expand Down
Loading