Skip to content

Assertion reached in SEMA related to builtin template specialization #4875

Closed

Description

Hi,

Reached this assert while trying to reproduce #4856

revision: 2c3d965
command line: ./build/bin/dxc -T cs_6_0 -E main repro.hlsl

code:

[[vk::binding(0)]] Texture2D<float> g_depth_buffer : register(t0);

#define A_GPU
#define A_HLSL
#include "ffx_a.h"

groupshared float g_group_shared_depth_values[16][16];

AF4 SpdLoadIntermediate(AU1 x, AU1 y) {
	float f = g_group_shared_depth_values[x][y];
	return f.xxxx;
}


[numthreads(32, 8, 1)]
void main(uint3 dispatch_thread_id : SV_DispatchThreadID, uint3 group_id : SV_GroupID, uint group_index : SV_GroupIndex){
}

I do not have a ffx_a.h file on my machine, at least not that I know of. Removing the first line makes DXC fails with the "ffx_a.h not found".

Error:

dxc: SemaHLSL.cpp:788: clang::QualType GetOrCreateTemplateSpecialization(clang::ASTContext &, clang::Sema &, clang::ClassTemplateDecl *, ArrayRef<clang::TemplateArgument>): Assertion `false == sema.InstantiateClassTemplateSpecialization( NoLoc, specializationDecl, TemplateSpecializationKind::TSK_ImplicitInstantiation, true)' failed.
Aborted

Activity

  1. added
    bug
    on Dec 13, 2022
  2. pow2clk commented on Dec 13, 2022

    @pow2clk
    Collaborator

    Not limited to SPIRV. even without the [[vk::binding]] annotation and targetting DXIL, this is an issue.

    Tex Riddell (@tex3d) speculates that it is likely failing to completely abort when reaching the failure of the missing include. Instead, it continues to try to compile skipping crucial steps so it hits this assert.

    The shader is pretty fiddly, but I was able to reduce it a bit:

    Texture2D<float> g_depth_buffer : register(t0);
    
    #include "nonexistent.h"
    
    groupshared float g_group_shared_depth_values[16][16];
    
    float LoadGS(float x, float y) {
    	float f = g_group_shared_depth_values[x][y];
    	return f.xxxx;
    }
    
    void main(){}

    compile as -T vs_6_0

    Removing almost any of these lines except for the last one will remove the repro.

  3. llvm-beanz commented on Dec 14, 2022

    @llvm-beanz
    Collaborator

    The issue reproduces on the release-1.7.2207 tag, so this is not new and is unrelated to the change replacing exceptions in the FileIO path.

  4. llvm-beanz commented on Dec 14, 2022

    @llvm-beanz
    Collaborator

    I think I see what is happening here...

    The assert that is firing is caused by failing to instantiate the vector type created by the f.xxxx. The instantiation seems to be failing here. Which I think is the caused by the include failure.

    The failed include causes the instantiating template to be marked as invalid, which results in the instantiation failing. We then assert on the failed instantiation instead of reasonably propagating the error.

    The method the assert is in is not written to propagate errors, so we should fix this by refactoring that method and its call sites to propagate errors properly, which may take some work.

  5. harshpg commented on Dec 19, 2022

    @harshpg
    Contributor

    Chris B (@llvm-beanz)

    I'm curious why [[vk::binding(0)]] Texture2D<float> g_depth_buffer : register(t0); has an effect on the assertion behavior. Removing this line makes the missing include be handled correctly.

  6. llvm-beanz commented on Dec 19, 2022

    @llvm-beanz
    Collaborator

    HLSL's built-in templates (like Texture2D) aren't fully formed AST types. To make them work and go through instantiation there's a lot of special logic that only applies (or should only apply) for those types. The assert being hit is in the logic that instantiates the resource template. If you have no resource template declarations in the file you won't hit the assert because you don't execute the function.

    The bug is that the HLSL-specific code for instantiating resource templates assumes that after DXC has already validated the template parameters through its own logic the actual AST-level instantiation can't fail. Which isn't a wholly unreasonable assumption, but it is a false assumption.

    The failure occurs because in C++ template instantiation can be really slow, and the resolution rules can vary contextually. Because of those two reasons, Clang returns failure (without a specific error) when instantiating a template after a fatal error has occurred in the compilation. This decision prioritizes giving an actionable error fast rather than spewing cascading nonsensical errors that are common in template instantiation errors, and is pretty defensibly the right decision.

    The end result is that if the include fails, the failure is identified and logged correctly, but when the template instantiation fails we hit an assert (which depending on platform and build settings either becomes a hard error or works as expected).

    IMO, the core issue here is one of best practices. I don't think it is ever a good programming practice to ignore a possible error return. Some exceptions can be made in cases where you're 100% confident you've verified that the error can't possibly happen (through sufficient checking ahead of the call), but we should be exceedingly careful when we do that. Ignoring error returns from complicated pieces of logic like template instantiation is probably never a good idea due to the underlying complexity.

  7. llvm-beanz commented on Aug 21, 2024

    @llvm-beanz
    Collaborator

    This should be fixed by the associated PR.

  8. llvm-beanz commented on Aug 21, 2024

    @llvm-beanz
    Collaborator

    Assigned to Tim Corringham (@tcorringham) since his PR should resolve this.

  9. added a commit that references this issue on Jul 21, 2026
    628c4d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions