From e86cd31065b3a2008a98a1e647d051ea59cd88ae Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Sat, 6 Dec 2025 14:23:11 +0100 Subject: [PATCH 1/5] Added flag for disabling HLSL intrinsics --- include/dxc/Support/HLSLOptions.td | 3 +++ include/dxc/Support/SPIRVOptions.h | 1 + lib/DxcSupport/HLSLOptions.cpp | 2 ++ 3 files changed, 6 insertions(+) diff --git a/include/dxc/Support/HLSLOptions.td b/include/dxc/Support/HLSLOptions.td index cd7dfb2f0c..5f6f7d4848 100644 --- a/include/dxc/Support/HLSLOptions.td +++ b/include/dxc/Support/HLSLOptions.td @@ -601,3 +601,6 @@ def rw_decl_global_cb : Flag<["-", "/"], "decl-global-cb">, Group; // 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, Flags<[CoreOption, DriverOption]>, + HelpText<"Disable HLSL intrinsics">; diff --git a/include/dxc/Support/SPIRVOptions.h b/include/dxc/Support/SPIRVOptions.h index 352cf6c2ec..615ebfd444 100644 --- a/include/dxc/Support/SPIRVOptions.h +++ b/include/dxc/Support/SPIRVOptions.h @@ -38,6 +38,7 @@ enum class SpirvLayoutRule { }; struct SpirvCodeGenOptions { + bool devshDisableHLSLIntrinsics; /// Disable legalization and optimization and emit raw SPIR-V bool codeGenHighLevel; bool debugInfoFile; diff --git a/lib/DxcSupport/HLSLOptions.cpp b/lib/DxcSupport/HLSLOptions.cpp index 0c9330b1d1..89a12bf1aa 100644 --- a/lib/DxcSupport/HLSLOptions.cpp +++ b/lib/DxcSupport/HLSLOptions.cpp @@ -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); From f861f419c5da393f344abf2ddf878e6bb4d98ff6 Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Mon, 8 Dec 2025 14:25:03 +0100 Subject: [PATCH 2/5] Implemented intrinsic disabling logic --- tools/clang/lib/Sema/SemaHLSL.cpp | 348 ++++++++++++++++++++++++++++++ 1 file changed, 348 insertions(+) diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index e9c8c90a2d..12f398cde9 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -5131,12 +5131,360 @@ class HLSLExternalSource : public ExternalSemaSource { bool IsValidObjectElement(LPCSTR tableName, IntrinsicOp op, QualType objectElement); + static bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier) + { + static const std::unordered_set forbiddenHLSLIntrinsics = { + "D3DCOLORtoUBYTE4", + "Abort", + "AcceptHitAndEndSearch", + "AddUint64", + "AllocateRayQuery", + "AllMemoryBarrier", + "AllMemoryBarrierWithGroupSync", + "Append", + "Barrier", + "CallShader", + "CalculateLevelOfDetail", + "CalculateLevelOfDetailUnclamped", + "CheckAccessFullyMapped", + "CommitNonOpaqueTriangleHit", + "CommitProceduralPrimitiveHit", + "CommittedInstanceContributionToHitGroupIndex", + "CommittedInstanceID", + "CommittedInstanceIndex", + "CommittedObjectRayDirection", + "CommittedObjectRayOrigin", + "CommittedObjectToWorld3x4", + "CommittedObjectToWorld4x3", + "CommittedPrimitiveIndex", + "CommittedRayT", + "CommittedStatus", + "CommittedTriangleBarycentrics", + "CommittedTriangleFrontFace", + "CommittedWorldToObject3x4", + "CommittedWorldToObject4x3", + "Consume", + "Count", + "CreateResourceFromHeap", + "CandidateGeometryIndex", + "CandidateInstanceContributionToHitGroupIndex", + "CandidateInstanceID", + "CandidateInstanceIndex", + "CandidateObjectRayDirection", + "CandidateObjectRayOrigin", + "CandidateObjectToWorld3x4", + "CandidateObjectToWorld4x3", + "CandidatePrimitiveIndex", + "CandidateProceduralPrimitiveNonOpaque", + "CandidateTriangleBarycentrics", + "CandidateTriangleFrontFace", + "CandidateTriangleRayT", + "CandidateType", + "CandidateWorldToObject3x4", + "CandidateWorldToObject4x3", + "DispatchMesh", + "DispatchRaysDimensions", + "DispatchRaysIndex", + "EvaluateAttributeAtSample", + "EvaluateAttributeCentroid", + "EvaluateAttributeSnapped", + "FinishedCrossGroupSharing", + "FromRayQuery", + "Gather", + "GatherAlpha", + "GatherBlue", + "GatherCmp", + "GatherCmpAlpha", + "GatherCmpBlue", + "GatherCmpGreen", + "GatherCmpRed", + "GatherGreen", + "GatherRaw", + "GatherRed", + "GeometryIndex", + "GetAttributeAtVertex", + "GetBufferContents", + "GetDimensions", + "GetHitKind", + "GetInstanceID", + "GetInstanceIndex", + "GetObjectRayDirection", + "GetObjectRayOrigin", + "GetObjectToWorld3x4", + "GetObjectToWorld4x3", + "GetPrimitiveIndex", + "GetRayFlags", + "GetRayTCurrent", + "GetRayTMin", + "GetRemainingRecursionLevels", + "GetRenderTargetSampleCount", + "GetRenderTargetSamplePosition", + "GetSamplePosition", + "GetShaderTableIndex", + "GetWorldRayDirection", + "GetWorldRayOrigin", + "GetWorldToObject3x4", + "GetWorldToObject4x3", + "HitKind", + "IgnoreHit", + "IncrementCounter", + "InstanceID", + "InstanceIndex", + "InterlockedAdd", + "InterlockedAdd64", + "InterlockedAnd", + "InterlockedAnd64", + "InterlockedCompareExchange", + "InterlockedCompareExchange64", + "InterlockedCompareExchangeFloatBitwise", + "InterlockedCompareStore", + "InterlockedCompareStore64", + "InterlockedCompareStoreFloatBitwise", + "InterlockedExchange", + "InterlockedExchange64", + "InterlockedExchangeFloat", + "InterlockedMax", + "InterlockedMax64", + "InterlockedMin", + "InterlockedMin64", + "InterlockedOr", + "InterlockedOr64", + "InterlockedXor", + "InterlockedXor64", + "Invoke", + "IsHit", + "IsMiss", + "IsNop", + "IsValid", + "Load", + "LoadLocalRootTableConstant", + "MakeMiss", + "MakeNop", + "MaybeReorderThread", + "NonUniformResourceIndex", + "ObjectRayDirection", + "ObjectRayOrigin", + "ObjectToWorld", + "ObjectToWorld3x4", + "ObjectToWorld4x3", + "OutputComplete", + "PrimitiveIndex", + "Proceed", + "Process2DQuadTessFactorsAvg", + "Process2DQuadTessFactorsMax", + "Process2DQuadTessFactorsMin", + "ProcessIsolineTessFactors", + "ProcessQuadTessFactorsAvg", + "ProcessQuadTessFactorsMax", + "ProcessQuadTessFactorsMin", + "ProcessTriTessFactorsAvg", + "ProcessTriTessFactorsMax", + "ProcessTriTessFactorsMin", + "QuadAll", + "QuadAny", + "QuadReadAcrossDiagonal", + "QuadReadAcrossX", + "QuadReadAcrossY", + "QuadReadLaneAt", + "RawBufferLoad", + "RawBufferStore", + "RayFlags", + "RayTCurrent", + "RayTMin", + "ReadClock", + "ReportHit", + "RestartStrip", + "SetMeshOutputCounts", + "SubpassLoad", + "TraceRay", + "TraceRayInline", + "WorldRayDirection", + "WorldRayOrigin", + "WorldToObject", + "WorldToObject3x4", + "WorldToObject4x3", + "WriteSamplerFeedback", + "WriteSamplerFeedbackBias", + "WriteSamplerFeedbackGrad", + "WriteSamplerFeedbackLevel", + "__builtin_MatVecMul", + "__builtin_MatVecMulAdd", + "__builtin_OuterProductAccumulate", + "__builtin_VectorAccumulate", + "abort", + "abs", + "acos", + "all", + "and", + "any", + "asdouble", + "asfloat", + "asfloat16", + "asin", + "asint", + "asint16", + "asuint", + "asuint16", + "atan", + "atan2", + "ceil", + "clamp", + "clip", + "cos", + "cosh", + "countbits", + "cross", + "ddx", + "ddx_coarse", + "ddx_fine", + "ddy", + "ddy_coarse", + "ddy_fine", + "degrees", + "determinant", + "distance", + "dot", + "dot2add", + "dot4add_i8packed", + "dot4add_u8packed", + "dst", + "exp", + "exp2", + "ext_execution_mode", + "ext_execution_mode_id", + "f16tof32", + "f32tof16", + "faceforward", + "firstbithigh", + "firstbitlow", + "floor", + "fma", + "fmod", + "frac", + "frexp", + "fwidth", + "isfinite", + "isinf", + "isnan", + "isnormal", + "ldexp", + "length", + "lerp", + "lit", + "log", + "log10", + "log2", + "mad", + "max", + "min", + "modf", + "msad4", + "mul", + "normalize", + "or", + "pack_clamp_s8", + "pack_clamp_u8", + "pack_s8", + "pack_u8", + "pow", + "printf", + "radians", + "rcp", + "reflect", + "refract", + "reinterpret_pointer_cast", + "reversebits", + "round", + "rsqrt", + "saturate", + "select", + "sign", + "sin", + "sincos", + "sinh", + "smoothstep", + "source_mark", + "sqrt", + "static_pointer_cast", + "step", + "tan", + "tanh", + "tex1D", + "tex1Dbias", + "tex1Dgrad", + "tex1Dlod", + "tex1Dproj", + "tex2D", + "tex2Dbias", + "tex2Dgrad", + "tex2Dlod", + "tex2Dproj", + "tex3D", + "tex3Dbias", + "tex3Dgrad", + "tex3Dlod", + "tex3Dproj", + "texCUBE", + "texCUBEbias", + "texCUBEgrad", + "texCUBElod", + "texCUBEproj", + "transpose", + "trunc", + "unpack_s8s16", + "unpack_s8s32", + "unpack_u8u16", + "unpack_u8u32", + "WaveActiveAllEqual", + "WaveActiveAllTrue", + "WaveActiveAnyTrue", + "WaveActiveBallot", + "WaveActiveBitAnd", + "WaveActiveBitOr", + "WaveActiveBitXor", + "WaveActiveCountBits", + "WaveActiveMax", + "WaveActiveMin", + "WaveActiveProduct", + "WaveActiveSum", + "WaveGetLaneCount", + "WaveGetLaneIndex", + "WaveIsFirstLane", + "WaveMatch", + "WaveMultiPrefixBitAnd", + "WaveMultiPrefixBitOr", + "WaveMultiPrefixBitXor", + "WaveMultiPrefixCountBits", + "WaveMultiPrefixProduct", + "WaveMultiPrefixSum", + "WavePrefixCountBits", + "WavePrefixProduct", + "WavePrefixSum", + "WaveReadLaneAt", + "WaveReadLaneFirst" + + }; + + auto it = forbiddenHLSLIntrinsics.find(std::string(intrinsicNameIdentifier)); + + return it == forbiddenHLSLIntrinsics.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. From 403a4d9fad273e2b0e4e9a720f33fd78917aab74 Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Mon, 8 Dec 2025 16:53:23 +0100 Subject: [PATCH 3/5] Now certain HLSL intrinsics will be disabled when the `devsh-disable-hlsl-intrinsics` flag is set --- tools/clang/include/clang/Sema/SemaHLSL.h | 13 + tools/clang/lib/Sema/SemaHLSL.cpp | 258 +----------------- .../clang/tools/dxcompiler/dxcompilerobj.cpp | 4 + 3 files changed, 26 insertions(+), 249 deletions(-) diff --git a/tools/clang/include/clang/Sema/SemaHLSL.h b/tools/clang/include/clang/Sema/SemaHLSL.h index 80ce8ddd7d..7558858ba4 100644 --- a/tools/clang/include/clang/Sema/SemaHLSL.h +++ b/tools/clang/include/clang/Sema/SemaHLSL.h @@ -24,6 +24,19 @@ #include "clang/Sema/Template.h" #include "clang/Sema/TemplateDeduction.h" +#include +extern std::optional disableHLSLIntrinsicsGlobalVariableBecauseIDontCare; + +inline bool shouldDisableHLSLIntrinsics() +{ + assert(disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value()); + + if (!disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value()) + exit(-1); + + return disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.value(); +} + // Forward declarations. struct IDxcIntrinsicTable; namespace clang { diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index 12f398cde9..f4c14e4e1f 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -53,6 +53,8 @@ #include #include +std::optional disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = std::nullopt; + enum ArBasicKind { AR_BASIC_BOOL, AR_BASIC_LITERAL_FLOAT, @@ -5133,22 +5135,14 @@ class HLSLExternalSource : public ExternalSemaSource { static bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier) { - static const std::unordered_set forbiddenHLSLIntrinsics = { - "D3DCOLORtoUBYTE4", + static const std::unordered_set allowedHLSLIntrinsics = { "Abort", "AcceptHitAndEndSearch", - "AddUint64", "AllocateRayQuery", - "AllMemoryBarrier", - "AllMemoryBarrierWithGroupSync", - "Append", - "Barrier", "CallShader", - "CalculateLevelOfDetail", - "CalculateLevelOfDetailUnclamped", - "CheckAccessFullyMapped", "CommitNonOpaqueTriangleHit", "CommitProceduralPrimitiveHit", + "CommittedGeometryIndex", "CommittedInstanceContributionToHitGroupIndex", "CommittedInstanceID", "CommittedInstanceIndex", @@ -5163,9 +5157,6 @@ class HLSLExternalSource : public ExternalSemaSource { "CommittedTriangleFrontFace", "CommittedWorldToObject3x4", "CommittedWorldToObject4x3", - "Consume", - "Count", - "CreateResourceFromHeap", "CandidateGeometryIndex", "CandidateInstanceContributionToHitGroupIndex", "CandidateInstanceID", @@ -5182,29 +5173,11 @@ class HLSLExternalSource : public ExternalSemaSource { "CandidateType", "CandidateWorldToObject3x4", "CandidateWorldToObject4x3", - "DispatchMesh", "DispatchRaysDimensions", "DispatchRaysIndex", - "EvaluateAttributeAtSample", - "EvaluateAttributeCentroid", - "EvaluateAttributeSnapped", - "FinishedCrossGroupSharing", "FromRayQuery", - "Gather", - "GatherAlpha", - "GatherBlue", - "GatherCmp", - "GatherCmpAlpha", - "GatherCmpBlue", - "GatherCmpGreen", - "GatherCmpRed", - "GatherGreen", - "GatherRaw", - "GatherRed", "GeometryIndex", - "GetAttributeAtVertex", - "GetBufferContents", - "GetDimensions", + "GetGeometryIndex", "GetHitKind", "GetInstanceID", "GetInstanceIndex", @@ -5216,10 +5189,6 @@ class HLSLExternalSource : public ExternalSemaSource { "GetRayFlags", "GetRayTCurrent", "GetRayTMin", - "GetRemainingRecursionLevels", - "GetRenderTargetSampleCount", - "GetRenderTargetSamplePosition", - "GetSamplePosition", "GetShaderTableIndex", "GetWorldRayDirection", "GetWorldRayOrigin", @@ -5227,247 +5196,38 @@ class HLSLExternalSource : public ExternalSemaSource { "GetWorldToObject4x3", "HitKind", "IgnoreHit", - "IncrementCounter", "InstanceID", "InstanceIndex", - "InterlockedAdd", - "InterlockedAdd64", - "InterlockedAnd", - "InterlockedAnd64", - "InterlockedCompareExchange", - "InterlockedCompareExchange64", - "InterlockedCompareExchangeFloatBitwise", - "InterlockedCompareStore", - "InterlockedCompareStore64", - "InterlockedCompareStoreFloatBitwise", - "InterlockedExchange", - "InterlockedExchange64", - "InterlockedExchangeFloat", - "InterlockedMax", - "InterlockedMax64", - "InterlockedMin", - "InterlockedMin64", - "InterlockedOr", - "InterlockedOr64", - "InterlockedXor", - "InterlockedXor64", "Invoke", "IsHit", "IsMiss", "IsNop", - "IsValid", - "Load", "LoadLocalRootTableConstant", "MakeMiss", "MakeNop", "MaybeReorderThread", - "NonUniformResourceIndex", "ObjectRayDirection", "ObjectRayOrigin", "ObjectToWorld", "ObjectToWorld3x4", "ObjectToWorld4x3", - "OutputComplete", "PrimitiveIndex", "Proceed", - "Process2DQuadTessFactorsAvg", - "Process2DQuadTessFactorsMax", - "Process2DQuadTessFactorsMin", - "ProcessIsolineTessFactors", - "ProcessQuadTessFactorsAvg", - "ProcessQuadTessFactorsMax", - "ProcessQuadTessFactorsMin", - "ProcessTriTessFactorsAvg", - "ProcessTriTessFactorsMax", - "ProcessTriTessFactorsMin", - "QuadAll", - "QuadAny", - "QuadReadAcrossDiagonal", - "QuadReadAcrossX", - "QuadReadAcrossY", - "QuadReadLaneAt", - "RawBufferLoad", - "RawBufferStore", "RayFlags", "RayTCurrent", "RayTMin", - "ReadClock", "ReportHit", - "RestartStrip", - "SetMeshOutputCounts", - "SubpassLoad", "TraceRay", "TraceRayInline", "WorldRayDirection", "WorldRayOrigin", "WorldToObject", "WorldToObject3x4", - "WorldToObject4x3", - "WriteSamplerFeedback", - "WriteSamplerFeedbackBias", - "WriteSamplerFeedbackGrad", - "WriteSamplerFeedbackLevel", - "__builtin_MatVecMul", - "__builtin_MatVecMulAdd", - "__builtin_OuterProductAccumulate", - "__builtin_VectorAccumulate", - "abort", - "abs", - "acos", - "all", - "and", - "any", - "asdouble", - "asfloat", - "asfloat16", - "asin", - "asint", - "asint16", - "asuint", - "asuint16", - "atan", - "atan2", - "ceil", - "clamp", - "clip", - "cos", - "cosh", - "countbits", - "cross", - "ddx", - "ddx_coarse", - "ddx_fine", - "ddy", - "ddy_coarse", - "ddy_fine", - "degrees", - "determinant", - "distance", - "dot", - "dot2add", - "dot4add_i8packed", - "dot4add_u8packed", - "dst", - "exp", - "exp2", - "ext_execution_mode", - "ext_execution_mode_id", - "f16tof32", - "f32tof16", - "faceforward", - "firstbithigh", - "firstbitlow", - "floor", - "fma", - "fmod", - "frac", - "frexp", - "fwidth", - "isfinite", - "isinf", - "isnan", - "isnormal", - "ldexp", - "length", - "lerp", - "lit", - "log", - "log10", - "log2", - "mad", - "max", - "min", - "modf", - "msad4", - "mul", - "normalize", - "or", - "pack_clamp_s8", - "pack_clamp_u8", - "pack_s8", - "pack_u8", - "pow", - "printf", - "radians", - "rcp", - "reflect", - "refract", - "reinterpret_pointer_cast", - "reversebits", - "round", - "rsqrt", - "saturate", - "select", - "sign", - "sin", - "sincos", - "sinh", - "smoothstep", - "source_mark", - "sqrt", - "static_pointer_cast", - "step", - "tan", - "tanh", - "tex1D", - "tex1Dbias", - "tex1Dgrad", - "tex1Dlod", - "tex1Dproj", - "tex2D", - "tex2Dbias", - "tex2Dgrad", - "tex2Dlod", - "tex2Dproj", - "tex3D", - "tex3Dbias", - "tex3Dgrad", - "tex3Dlod", - "tex3Dproj", - "texCUBE", - "texCUBEbias", - "texCUBEgrad", - "texCUBElod", - "texCUBEproj", - "transpose", - "trunc", - "unpack_s8s16", - "unpack_s8s32", - "unpack_u8u16", - "unpack_u8u32", - "WaveActiveAllEqual", - "WaveActiveAllTrue", - "WaveActiveAnyTrue", - "WaveActiveBallot", - "WaveActiveBitAnd", - "WaveActiveBitOr", - "WaveActiveBitXor", - "WaveActiveCountBits", - "WaveActiveMax", - "WaveActiveMin", - "WaveActiveProduct", - "WaveActiveSum", - "WaveGetLaneCount", - "WaveGetLaneIndex", - "WaveIsFirstLane", - "WaveMatch", - "WaveMultiPrefixBitAnd", - "WaveMultiPrefixBitOr", - "WaveMultiPrefixBitXor", - "WaveMultiPrefixCountBits", - "WaveMultiPrefixProduct", - "WaveMultiPrefixSum", - "WavePrefixCountBits", - "WavePrefixProduct", - "WavePrefixSum", - "WaveReadLaneAt", - "WaveReadLaneFirst" - + "WorldToObject4x3" }; - auto it = forbiddenHLSLIntrinsics.find(std::string(intrinsicNameIdentifier)); - - return it == forbiddenHLSLIntrinsics.end(); + auto it = allowedHLSLIntrinsics.find(std::string(intrinsicNameIdentifier)); + return it != allowedHLSLIntrinsics.end(); } // Returns the iterator with the first entry that matches the requirement @@ -5477,7 +5237,7 @@ class HLSLExternalSource : public ExternalSemaSource { StringRef nameIdentifier, size_t argumentCount) { // TODO: only check if the flag "devsh-disable-hlsl-intrinsics" is enabled - if (!checkIfIntrinsicIsAllowed(nameIdentifier)) + if (shouldDisableHLSLIntrinsics() && !checkIfIntrinsicIsAllowed(nameIdentifier)) { return IntrinsicDefIter::CreateStart( table, tableSize, table + tableSize, diff --git a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp index 133e40c5bd..40b9a74e6d 100644 --- a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp +++ b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp @@ -9,6 +9,8 @@ // // /////////////////////////////////////////////////////////////////////////////// +#include "clang/Sema/SemaHLSL.h" + #include "clang/Basic/Diagnostic.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" @@ -626,6 +628,8 @@ class DxcCompiler : public IDxcCompiler3, static_cast(pOrigUtf8Source->GetStringPointer()), pOrigUtf8Source->GetStringLength()); + disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = opts.SpirvOptions.devshDisableHLSLIntrinsics; + CComPtr pSrcCodeResult; std::vector PreprocessArgs; PreprocessArgs.reserve(argCount + 1); From 7a2f900d2b2eb9f5969b515a0f26797ed38241ad Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Thu, 11 Dec 2025 13:48:13 +0100 Subject: [PATCH 4/5] Removed global flag --- tools/clang/include/clang/AST/ASTContext.h | 2 +- tools/clang/include/clang/AST/HlslTypes.h | 3 ++- .../include/clang/Frontend/CompilerInstance.h | 2 +- .../include/clang/Frontend/FrontendAction.h | 3 ++- tools/clang/include/clang/Sema/SemaHLSL.h | 13 ---------- tools/clang/lib/AST/ASTContext.cpp | 4 +-- tools/clang/lib/Frontend/CompilerInstance.cpp | 4 +-- tools/clang/lib/Frontend/FrontendAction.cpp | 5 ++-- tools/clang/lib/Sema/SemaHLSL.cpp | 26 ++++++++++--------- .../clang/tools/dxcompiler/dxcompilerobj.cpp | 5 ++-- 10 files changed, 29 insertions(+), 38 deletions(-) diff --git a/tools/clang/include/clang/AST/ASTContext.h b/tools/clang/include/clang/AST/ASTContext.h index ab0e33513b..59403fa73f 100644 --- a/tools/clang/include/clang/AST/ASTContext.h +++ b/tools/clang/include/clang/AST/ASTContext.h @@ -2408,7 +2408,7 @@ class ASTContext : public RefCountedBase { /// 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); diff --git a/tools/clang/include/clang/AST/HlslTypes.h b/tools/clang/include/clang/AST/HlslTypes.h index 43c1effdb8..d9b2ca35de 100644 --- a/tools/clang/include/clang/AST/HlslTypes.h +++ b/tools/clang/include/clang/AST/HlslTypes.h @@ -51,7 +51,8 @@ namespace hlsl { /// Initializes the specified context to support HLSL /// compilation. -void InitializeASTContextForHLSL(clang::ASTContext &context); +void InitializeASTContextForHLSL(clang::ASTContext &context, + bool ignoreHLSLIntrinsics); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Type system enumerations. diff --git a/tools/clang/include/clang/Frontend/CompilerInstance.h b/tools/clang/include/clang/Frontend/CompilerInstance.h index 3c8270f9a0..ac087458f3 100644 --- a/tools/clang/include/clang/Frontend/CompilerInstance.h +++ b/tools/clang/include/clang/Frontend/CompilerInstance.h @@ -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. diff --git a/tools/clang/include/clang/Frontend/FrontendAction.h b/tools/clang/include/clang/Frontend/FrontendAction.h index c407ff80ac..83e065de6d 100644 --- a/tools/clang/include/clang/Frontend/FrontendAction.h +++ b/tools/clang/include/clang/Frontend/FrontendAction.h @@ -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(); diff --git a/tools/clang/include/clang/Sema/SemaHLSL.h b/tools/clang/include/clang/Sema/SemaHLSL.h index 7558858ba4..80ce8ddd7d 100644 --- a/tools/clang/include/clang/Sema/SemaHLSL.h +++ b/tools/clang/include/clang/Sema/SemaHLSL.h @@ -24,19 +24,6 @@ #include "clang/Sema/Template.h" #include "clang/Sema/TemplateDeduction.h" -#include -extern std::optional disableHLSLIntrinsicsGlobalVariableBecauseIDontCare; - -inline bool shouldDisableHLSLIntrinsics() -{ - assert(disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value()); - - if (!disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.has_value()) - exit(-1); - - return disableHLSLIntrinsicsGlobalVariableBecauseIDontCare.value(); -} - // Forward declarations. struct IDxcIntrinsicTable; namespace clang { diff --git a/tools/clang/lib/AST/ASTContext.cpp b/tools/clang/lib/AST/ASTContext.cpp index 2445a421fe..e2c50027d9 100644 --- a/tools/clang/lib/AST/ASTContext.cpp +++ b/tools/clang/lib/AST/ASTContext.cpp @@ -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?"); @@ -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 } diff --git a/tools/clang/lib/Frontend/CompilerInstance.cpp b/tools/clang/lib/Frontend/CompilerInstance.cpp index c39ff51b1d..f26b157a45 100644 --- a/tools/clang/lib/Frontend/CompilerInstance.cpp +++ b/tools/clang/lib/Frontend/CompilerInstance.cpp @@ -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 diff --git a/tools/clang/lib/Frontend/FrontendAction.cpp b/tools/clang/lib/Frontend/FrontendAction.cpp index 54ce6f154b..ae2589f07d 100644 --- a/tools/clang/lib/Frontend/FrontendAction.cpp +++ b/tools/clang/lib/Frontend/FrontendAction.cpp @@ -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); @@ -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 Consumer = CreateWrappedASTConsumer(CI, InputFile); diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index f4c14e4e1f..c5c3542642 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -53,8 +53,6 @@ #include #include -std::optional disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = std::nullopt; - enum ArBasicKind { AR_BASIC_BOOL, AR_BASIC_LITERAL_FLOAT, @@ -2996,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) @@ -4115,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)); @@ -5133,8 +5135,11 @@ class HLSLExternalSource : public ExternalSemaSource { bool IsValidObjectElement(LPCSTR tableName, IntrinsicOp op, QualType objectElement); - static bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier) + bool checkIfIntrinsicIsAllowed(StringRef intrinsicNameIdentifier) { + if (!m_disableHLSLIntrinsics) + return true; + static const std::unordered_set allowedHLSLIntrinsics = { "Abort", "AcceptHitAndEndSearch", @@ -5220,10 +5225,7 @@ class HLSLExternalSource : public ExternalSemaSource { "TraceRay", "TraceRayInline", "WorldRayDirection", - "WorldRayOrigin", - "WorldToObject", - "WorldToObject3x4", - "WorldToObject4x3" + "WorldRayOrigin" }; auto it = allowedHLSLIntrinsics.find(std::string(intrinsicNameIdentifier)); @@ -5237,7 +5239,7 @@ class HLSLExternalSource : public ExternalSemaSource { StringRef nameIdentifier, size_t argumentCount) { // TODO: only check if the flag "devsh-disable-hlsl-intrinsics" is enabled - if (shouldDisableHLSLIntrinsics() && !checkIfIntrinsicIsAllowed(nameIdentifier)) + if (!checkIfIntrinsicIsAllowed(nameIdentifier)) { return IntrinsicDefIter::CreateStart( table, tableSize, table + tableSize, @@ -13560,8 +13562,8 @@ hlsl::TrySubscriptIndexInitialization(clang::Sema *self, clang::Expr *SrcExpr, /// Performs HLSL-specific initialization on the specified /// context. -void hlsl::InitializeASTContextForHLSL(ASTContext &context) { - HLSLExternalSource *hlslSource = new HLSLExternalSource(); +void hlsl::InitializeASTContextForHLSL(ASTContext &context, bool ignoreHLSLIntrinsics) { + HLSLExternalSource *hlslSource = new HLSLExternalSource(ignoreHLSLIntrinsics); IntrusiveRefCntPtr externalSource(hlslSource); if (hlslSource->Initialize(context)) { context.setExternalSource(externalSource); diff --git a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp index 40b9a74e6d..798be73e9b 100644 --- a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp +++ b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp @@ -628,8 +628,6 @@ class DxcCompiler : public IDxcCompiler3, static_cast(pOrigUtf8Source->GetStringPointer()), pOrigUtf8Source->GetStringLength()); - disableHLSLIntrinsicsGlobalVariableBecauseIDontCare = opts.SpirvOptions.devshDisableHLSLIntrinsics; - CComPtr pSrcCodeResult; std::vector PreprocessArgs; PreprocessArgs.reserve(argCount + 1); @@ -968,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.devshDisableHLSLIntrinsics); action.Execute(); action.EndSourceFile(); outStream.flush(); From 669f2af36c39d94c8e074e757aee576edb3a5c5a Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Thu, 11 Dec 2025 14:04:19 +0100 Subject: [PATCH 5/5] Renamed the flag --- include/dxc/Support/HLSLOptions.td | 2 +- include/dxc/Support/SPIRVOptions.h | 2 +- lib/DxcSupport/HLSLOptions.cpp | 4 ++-- tools/clang/tools/dxcompiler/dxcompilerobj.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/dxc/Support/HLSLOptions.td b/include/dxc/Support/HLSLOptions.td index 5f6f7d4848..85b64ffe55 100644 --- a/include/dxc/Support/HLSLOptions.td +++ b/include/dxc/Support/HLSLOptions.td @@ -602,5 +602,5 @@ def rw_decl_global_cb : Flag<["-", "/"], "decl-global-cb">, Group, Group, Flags<[CoreOption, DriverOption]>, +def fvk_disable_hlsl_intrinsics : Flag<["-"], "fvk-disable-hlsl-intrinsics">, Group, Flags<[CoreOption, DriverOption]>, HelpText<"Disable HLSL intrinsics">; diff --git a/include/dxc/Support/SPIRVOptions.h b/include/dxc/Support/SPIRVOptions.h index 615ebfd444..bd9a19b94f 100644 --- a/include/dxc/Support/SPIRVOptions.h +++ b/include/dxc/Support/SPIRVOptions.h @@ -38,7 +38,7 @@ enum class SpirvLayoutRule { }; struct SpirvCodeGenOptions { - bool devshDisableHLSLIntrinsics; + bool disableHLSLIntrinsics; /// Disable legalization and optimization and emit raw SPIR-V bool codeGenHighLevel; bool debugInfoFile; diff --git a/lib/DxcSupport/HLSLOptions.cpp b/lib/DxcSupport/HLSLOptions.cpp index 89a12bf1aa..a214dba7e6 100644 --- a/lib/DxcSupport/HLSLOptions.cpp +++ b/lib/DxcSupport/HLSLOptions.cpp @@ -1074,8 +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.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); diff --git a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp index 798be73e9b..11f68f28b8 100644 --- a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp +++ b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp @@ -967,7 +967,7 @@ class DxcCompiler : public IDxcCompiler3, clang::EmitSpirvAction action; FrontendInputFile file(pUtf8SourceName, IK_HLSL); action.BeginSourceFile( - compiler, file, opts.SpirvOptions.devshDisableHLSLIntrinsics); + compiler, file, opts.SpirvOptions.disableHLSLIntrinsics); action.Execute(); action.EndSourceFile(); outStream.flush();