Skip to content

[202x][0009] Implement hlsl namespace for intrinsic functions - #9006

Open
Chris B (llvm-beanz) wants to merge 16 commits into
microsoft:mainfrom
llvm-beanz:hlsl-namespace
Open

Chris B (llvm-beanz) wants to merge 16 commits into
microsoft:mainfrom
llvm-beanz:hlsl-namespace

Conversation

@llvm-beanz

@llvm-beanz Chris B (llvm-beanz) commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

This PR implements the hlsl namespace and moves all built-in functions into the hlsl namespace.

This feature was adopted by TC57 in proposal 0009: https://hlsl-tc57.github.io/tc57/proposal/0009/

This PR has significantly grown in complexity during development. Initially it just put declarations into the namespace and did some minor fixes to name lookup. As the test cases developed that proved insufficient because of how the names of functions are lazily added to the AST (which is critical for DXC's compile-time performance).

The full change has a more invasive connection into name lookup, and leverages another part of the ExternalSemaSource.

The first change is marking our namespaces with the ExternallyVisibleStorage, which is a flag on the DeclContext to mark that it has "some external storage" (think serialized AST file, or our code that injects AST nodes) that contains declarations which should be visible in the context. This is similar to the ExternalLexicalStorage flag which we already use, but it extends from just lexical declarations to visible declarations.

When name lookup inspects a declaration context there is a callback, FindExternalVisibleDeclsByName, which gets called if the DeclContext has externally stored visible declarations. This provides a hook to insert visible names during name lookup. This complements our existing name lookup hook in LookupUnqualified by also supporting qualified name lookup, and triggering earlier in the lookup process.

In FindExternalVisibleDeclsByName, if the specified name is an intrinsic we insert a function declaration that serves as a marker for the builtin intrinsic. This function declaration takes no arguments, returns void, and has an attribute applied to mark it as an intrinsic marker. This gives us a declaration to slide into normal name lookup, but also avoids us needing to add all the overloads.

When name lookup returns such a declaration we then know that we have an intrinsic function and we can perform the same overload resolution against it that we would have previously.

The one outstanding bit of work is to move the HLSL resource objects and raytracing objects into the hlsl namespace.

Partial implementation of #8918

This PR implements the `hlsl` namespace and moves all built-in functions
into the `hlsl` namespace.

This feature was adopted by TC57 in proposal 0009:
https://hlsl-tc57.github.io/tc57/proposal/0009/

Resolves microsoft#8918
Copilot AI balanced review requested due to automatic review settings October 6, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The AST test’s negative check does not currently verify the absence of an implicit using-directive throughout the output.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Implements TC57 proposal 0009 by moving HLSL 202x built-in functions into the implicit hlsl namespace while preserving earlier language behavior.

Changes:

  • Adds version-aware namespace lookup and intrinsic resolution.
  • Adds semantic and DXIL coverage for qualification and using-directives.
  • Updates affected tests and release notes.
File Description
tools/​clang/​lib/​Sema/​SemaHLSL.cpp Creates and resolves the hlsl namespace.
tools/​clang/​lib/​Sema/​SemaExpr.cpp Enables qualified intrinsic lookup.
tools/​clang/​lib/​Sema/​SemaOverload.cpp Supports overload resolution in hlsl.
docs/​ReleaseNotes.md Documents the language change.
tools/​clang/​test/​SemaHLSL/​v202x/​conforming-literals/​asfloat16.hlsl Qualifies asfloat16.
tools/​clang/​test/​SemaHLSL/​hlsl-using-namespace-tu-scope-202x.hlsl Tests translation-unit using-directives.
tools/​clang/​test/​SemaHLSL/​hlsl-using-namespace-namespace-scope-202x.hlsl Tests namespace-scoped using-directives.
tools/​clang/​test/​SemaHLSL/​hlsl-using-namespace-function-scope-202x.hlsl Tests block-scoped using-directives.
tools/​clang/​test/​SemaHLSL/​hlsl-namespace-qualified-intrinsics.hlsl Tests qualified and unqualified lookup.
tools/​clang/​test/​SemaHLSL/​hlsl-namespace-pre-202x.hlsl Verifies legacy AST behavior.
tools/​clang/​test/​SemaHLSL/​hlsl-namespace-not-available-pre-202x.hlsl Verifies legacy namespace behavior.
tools/​clang/​test/​SemaHLSL/​hlsl-namespace-no-implicit-using-202x.hlsl Tests absence of implicit imports.
tools/​clang/​test/​SemaHLSL/​hlsl-namespace-202x.hlsl Checks the HLSL 202x AST.
tools/​clang/​test/​HLSLFileCheckLit/​hlsl/​auto/​auto-variable-deduction.hlsl Adapts an existing 202x test.
tools/​clang/​test/​HLSLFileCheck/​hlsl/​control_flow/​attributes/​unroll/​full_hint_202x.hlsl Qualifies dot.
tools/​clang/​test/​HLSLFileCheck/​hlsl/​control_flow/​attributes/​unroll/​count_runtime_hint_202x.hlsl Qualifies runtime-loop dot calls.
tools/​clang/​test/​HLSLFileCheck/​hlsl/​control_flow/​attributes/​unroll/​count_hint_202x.hlsl Qualifies counted-loop dot.
tools/​clang/​test/​CodeGenDXIL/​hlsl/​intrinsics/​hlsl-using-namespace-call.hlsl Tests unqualified DXIL lowering via using.
tools/​clang/​test/​CodeGenDXIL/​hlsl/​intrinsics/​hlsl-namespace-qualified-call.hlsl Tests qualified DXIL lowering.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Update the test command to check for implicit 'hlsl' namespace.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 21:27
tools/clang/include/clang/Basic/DiagnosticCommonKinds.td
tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
tools/clang/test/CodeGenSPIRV/SampledTexture/SampledTexture2D/vk.sampled
texture2d.half4.error.hlsl
tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl
tools/clang/test/CodeGenSPIRV/implicit.global.ubo.hlsl
tools/clang/test/CodeGenSPIRV/resource-heap-ext-texture.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.alias-fn-readwrit
e.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.alias-return.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.alias-to-alias.hl
sl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.append-consume.er
ror.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.array-stride.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.buffer-alias-fn-p
aram.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.buffer-direct-fn-
param.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.buffer.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.chained-assignmen
t.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.constant-buffer-a
ssignment.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.constant-texture-
buffer.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.counter-ops-direc
t.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.counter-ops.error
.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.direct-return.err
or.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.discarded.error.h
lsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.function-params.h
lsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.gather.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.groupshared.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-file-
scope.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-fn-pa
ram-crossing.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-fn-pa
ram-forward.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-fn-pa
ram.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-fn-re
turn-forward.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.image-alias-fn-re
turn.error.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.load-offset.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.mixed-alias.error
.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.mixed-bound.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.nonuniform.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.rwbyteaddressbuff
er.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.rwtexture-atomics
.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.rwtexture-dims.hl
sl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.sample-grad-bias.
hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.sampler-compariso
n.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.static-global.hls
l
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.structured-buffer
-atomic.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.targetenv.error.h
lsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.texture-dims.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.texture-ms.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.texture-sampler-a
ssignment.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.texture.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.texturecube.hlsl
tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.ext.typed-formats.hls
l
tools/clang/test/CodeGenSPIRV/type.constant-buffer.fn-var.bitfield.hlsl
tools/clang/test/CodeGenSPIRV/type.rwtexture2dms.hlsl
tools/clang/test/CodeGenSPIRV/user-defined-type.builtin-name.buffers.hls
l
tools/clang/test/CodeGenSPIRV/user-defined-type.builtin-name.rayquery.hl
sl
tools/clang/test/CodeGenSPIRV/user-defined-type.builtin-name.textures.hl
sl
tools/clang/test/CodeGenSPIRV/var.init.warning.ignored.hlsl
tools/clang/test/DXC/Passes/DxilEliminateVector/debug-value.ll
tools/clang/test/DXC/Passes/DxilEliminateVector/worklist.ll
tools/clang/test/HLSL/rewriter/typemods-syntax_noerr.hlsl
tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl
tools/clang/test/HLSLFileCheck/pix/AccessTrackingBarrierIsNotAnAccess.hl
sl
tools/clang/test/HLSLFileCheck/pix/AccessTrackingLibAnnotateHandleIsNotA
nAccess.hlsl
tools/clang/test/HLSLFileCheck/pix/AccessTrackingLibAnnotatedHandleReadS
tillRecorded.hlsl
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl
tools/clang/test/HLSLFileCheckLit/hlsl/entry/groupshared-initializer.hls
l
tools/clang/test/HLSLFileCheckLit/reflection/rFooSpace.hlsl
tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl
tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl
tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl
tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-export-sm65.hlsl
tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl
tools/clang/test/SemaHLSL/removed-keywords-2026-warnings.hlsl
tools/clang/test/SemaHLSL/v2021-static-assert-not-keyword.hlsl
tools/clang/test/SemaHLSL/v202x/conforming-literals/valid-literals.hlsl
tools/clang/test/SemaHLSL/vector-syntax-exact-precision.hlsl

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Intrinsic lookup and constant-folding regressions remain unresolved.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve namespace visibility for dependent HLSL intrinsic calls

tools/​clang/​lib/​Sema/​SemaHLSL.cpp:5576

Dependent unqualified intrinsic calls lose access to hlsl during template instantiation. For example, template<typename T> T f(T x) { using namespace hlsl; return sin(x); } cannot resolve sin when first instantiated with float: TreeTransform::RebuildCallExpr passes a null Scope, leaving SearchHlslViaUsing false. A translation-unit using-directive has the same problem. Preserve definition-time namespace visibility for dependent calls, including block-scoped directives, and add regression tests for both scopes.

Medium severity Restore constant folding for HLSL namespace intrinsics

tools/​clang/​lib/​Sema/​SemaHLSL.cpp:5682

Making m_hlslNSDecl non-null drops the existing constant-folding path for core intrinsics. AddHLSLIntrinsicFunction attaches HLSLBuiltinCallAttr only when NS is null, while CGExpr.cpp:3649 requires that attribute to reach TryEvalIntrinsic. Thus constant calls such as hlsl::sin(1.0f) bypass codegen-time folding. After creating an intrinsic declaration, attach the attribute when m_hlslNSDecl && T.NS == m_hlslNSDecl, preserving existing dx/vk handling, and add a constant-argument codegen regression.

Medium severity Support HLSL intrinsics in using-declarations

tools/​clang/​lib/​Sema/​SemaHLSL.cpp:5689

In a fresh HLSL 202x translation unit, using hlsl::sin; fails with “no member named 'sin'”. Intrinsic declarations are synthesized by AddOverloadedCallCandidates, but BuildUsingDeclaration performs ordinary qualified lookup; marking external lexical storage does not expose those names. Support intrinsic-name lookup for using-declarations while preserving lazy overload generation, and add a regression that imports and calls an intrinsic before any qualified call.

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 19, 2026 21:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved lookup defects affect template instantiation, selective imports, and qualified-call namespace isolation.

3 open issues

Review effort: Balanced

Comment thread tools/clang/lib/Sema/SemaExpr.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
../tools/clang/test/SemaHLSL/Inputs/hlsl-namespace-overloads.h
../tools/clang/test/SemaHLSL/hlsl-namespace-header-overloads-202x.hlsl
../tools/clang/test/SemaHLSL/hlsl-namespace-using-declaration-202x.hlsl
Copilot AI balanced review requested due to automatic review settings October 21, 2026 21:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Intrinsic families found only through argument-dependent lookup bypass synthesis, rejecting valid unqualified calls.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The interaction between lazy intrinsic synthesis, namespace lookup, and overload resolution needs final human validation before approval.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The cross-cutting changes to lazy name lookup and overload resolution warrant final human review supported by compiler regression-test results.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The cross-cutting changes to lazy name lookup and overload resolution warrant compiler-maintainer review and regression-test validation.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Inconsistent argument-dependent lookup checks can abort assertion-enabled compilers on valid calls through nested dx or vk namespaces.

1 open issue

Review effort: Balanced

Comment thread tools/clang/lib/Sema/SemaOverload.cpp Resolved
Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The changes span lazy name lookup, template instantiation, and overload resolution, requiring compiler-domain human review supported by regression results.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@llvm-beanz Chris B (llvm-beanz) changed the title [202x][0009] Implement hlsl namespace [202x][0009] Implement hlsl namespace for intrinsic functions Oct 22, 2026

@V-FEXrt Ashley Coleman (V-FEXrt) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has some tricky stuff but overall reasonable for the required functionality. Tests do a great job covering all the cases so thanks for those:)

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved

@hekota Helena Kotas (hekota) left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - pending Sarah's feedback in SemaHLSL.cpp.

Comment thread tools/clang/lib/Sema/SemaExpr.cpp Resolved
Co-authored-by: Helena Kotas <hekotas@microsoft.com>
Copilot AI balanced review requested due to automatic review settings November 6, 2026 17:25
…x.hlsl

Co-authored-by: Helena Kotas <hekotas@microsoft.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because an intrinsic-overload edge case leaves no candidates for an argument-count mismatch, producing a misleading missing-member diagnostic.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 6, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human review is needed to assess the impact of duplicated argument-dependent lookup work on ordinary HLSL 202x user-function calls.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 6, 2026 18:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Interacting changes to core lookup, ADL, and template overload resolution require compiler-maintainer review and cross-version DXIL/SPIR-V regression validation beyond this source-only review.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

5 participants