HLSL doesn't have a runtime loader model that supports global
construction by a loader or runtime initializer. To allow us to leverage
global constructors with minimal code generation impact we put calls to
the global constructors inside the generated entry function.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
151 | Don't need to generate CtorCalls for lib profile in clang codeGen. |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
151 | Are you sure? The global constructors contain the createHandle calls for resources, and DXC does currently generate those inside shader entries for lib shaders. I'm sure we're not generating those _correctly_ with this change, but I think we still need the constructor calls inside any function annotated with the [shader(...)] attribute. |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
151 | I saw dxc did call ctor for all entries. Maybe to match the behavior, we need to do the same thing. |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
151 | I can (and should) add a library shader test too. In addition to the cases where DXC generates global constructors, we need them in clang in more cases, because I'm using global constructors to reduce the scope of changes HLSL needs in CodeGen. In the case of global resource variables (as shown in the test in this PR), DXC generates the handle creation calls with a bunch of custom code in clang CodeGen, and inserts that code into the entry functions. To avoid needing special code for handle creation in Clang, I've put handle creation calls inside the constructors for the resource object. For global definitions, the constructors then get called by the global initialization function automatically, which we can inline into the entry. |
clang/test/CodeGenHLSL/GlobalConstructors.hlsl | ||
---|---|---|
3 | Can you also add a test using __attribute__((constructor))? And probably one using __attribute__((destructor)) at some point as well? https://godbolt.org/z/jqedf1qGa I see the docs said that global destructors are not supported, which is fine, but then what's the behavior when a user tries one anyway? (I would expect we'd get reasonable errors about it not being supported). Feel free to handle the dtor bits in a follow-up though. |
clang/test/CodeGenHLSL/GlobalConstructors.hlsl | ||
---|---|---|
3 | I need to play with this. We don't currently support global destructors, but I think we're going to in Clang. I'll try to get a second patch up today or tomorrow to add global destructors. |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
167–170 | Do we have sema checks for this so users get decent diagnostics instead of a crash (or miscompile)? |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
167–170 | It looks like COMDats are only attached if object format supports them (and DXContainer isn't properly handled there). I don't think there is any sema-level validation because the COMDat just binds to the global value address. I'll get a patch up to llvm::Triple to mark DXContainer as not supporting COMDat. |
clang/lib/CodeGen/CGHLSLRuntime.cpp | ||
---|---|---|
167–170 | Thanks! Also be sure to add some logic to SemaDeclAttr.cpp to diagnose specifying a priority. |
Adding Sema errors for specifying initializer priority, and a test case for library shaders.
Don't need to generate CtorCalls for lib profile in clang codeGen.
Have to do this when linking anyway.