This is an archive of the discontinued LLVM Phabricator instance.

[HLSL] CodeGen hlsl cbuffer/tbuffer.
ClosedPublic

Authored by python3kgae on Jul 19 2022, 4:07 PM.

Details

Summary

cbuffer A {

float a;
float b;

}

will be translated to a global variable.

Something like

struct CB_Ty {

float a;
float b;

};

CB_Ty A;

And all use of a and b will be replaced with A.a and A.b.

Only support none-legacy cbuffer layout now.
CodeGen for Resource binding will be in separate patch.
In the separate patch, resource binding will map the resource information to the global variable.

Diff Detail

Event Timeline

python3kgae created this revision.Jul 19 2022, 4:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2022, 4:07 PM
python3kgae requested review of this revision.Jul 19 2022, 4:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2022, 4:07 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Anastasia added inline comments.Jul 27 2022, 9:04 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
65

I don't think I understand the intent of this function along with CGHLSLRuntime::addConstant that populates this collection.

66

It might be better to avoid using hard-coded constants. Are you adding new entires in clang's AddressSpace enum to represent different logical memory segment of the language?

163

Is this case covered by the test?

clang/lib/CodeGen/CGHLSLRuntime.h
53

what does this field represent?

python3kgae added inline comments.Jul 28 2022, 10:59 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
163

Nice catch.
I'll add a test for this.

clang/lib/CodeGen/CGHLSLRuntime.h
53

The field is for marking the CBuffer as a cbuffer or tbuffer.
The name is confusing.
How about adding an enum BufferKind { CBuffer, TBuffer };?

python3kgae added inline comments.Jul 28 2022, 10:59 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
65

It is translating

 cbuffer A {
   float a;
   float b;
}
float foo() {
  return a + b;
}

into

struct cb.A.Ty {
  float a;
  float b;
};

cbuffer A {
  cb.A.Ty CBA;
}
float foo() {
  return CBA.a + CBA.b;
}

Both a and b are in the global scope and will get a GlobalVariable in clang codeGen.
By doing the translation, we can ensure each buffer map to one GlobalVariable and save cbuffer layout in the value type of that GlobalVariable.

66

Thanks for pointing it out. I'll add the new address space to LangAS.

Take care alignment when layout CBuffer.
Add hlsl_cbuffer/tbuffer to clang::LangAS.
Change CGHLSLRuntime::CBuffer to CGHLSLRuntime::Buffer to match HLSLBufferDecl.

Add more comments.

python3kgae marked an inline comment as done.Jul 28 2022, 11:57 AM
python3kgae added inline comments.
clang/lib/CodeGen/CGHLSLRuntime.cpp
163

Cannot add a test before other HLSL resource type like Texture2D is supported :(

clang/lib/CodeGen/CGHLSLRuntime.h
53

Change struct CBuffer into struct Buffer to match HLSLBufferDecl.

beanz added inline comments.Jul 28 2022, 12:16 PM
clang/lib/CodeGen/CGHLSLRuntime.cpp
163

Texture2D will be a CXXRecordDecl, not a HLSLBufferDecl, we only need buffer decls for the legacy buffers. So this should be testable by nesting a cbuffer inside a cbuffer right?

python3kgae marked an inline comment as done.Jul 28 2022, 12:24 PM
python3kgae added inline comments.
clang/lib/CodeGen/CGHLSLRuntime.cpp
163

I see.
I was thinking about test the error path.
I'll add a test for nested cbuffer.

beanz added inline comments.Jul 28 2022, 12:27 PM
clang/lib/Basic/Targets/SPIR.h
46 ↗(On Diff #448407)

I don’t think you need this comment. Most of those adds space values aren’t applicable to these targets, that’s why they are all 0.

clang/lib/CodeGen/CGHLSLRuntime.cpp
276

Why look both of these values up when you’re only going to use one?

280

nit: Don’t use auto in places where the type isn’t obvious from the expression.

295

nit: Rather than using int max as a sentinel here, why not use an llvm::Optional?

python3kgae marked an inline comment as done.

Add test for nested cbuffer.

python3kgae marked 2 inline comments as done.

Remove useless comment.
Use llvm::Optional.
Use real type instead of auto.

python3kgae marked an inline comment as done.Jul 28 2022, 2:03 PM
python3kgae marked an inline comment as done.Jul 28 2022, 2:35 PM
Anastasia added inline comments.Jul 29 2022, 4:20 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
65

Ok, I see, so if we are to translate it to C it would be something similar to:

struct A {
   float a;
   float b;
} cbuffer_A __attribute__((address_space(256)));

float foo() {
  return cbuffer_A.a + cbuffer_A.b;
}

Maybe you can add some comments to explain the intent of this code at a higher level... not sure if the generation can reuse or be made a bit close to the regular C structs + address spaces...

124

btw is this covered in the test?

157

Ok I think you don't have this in the tests too and the one below?

274

I think it's better to use getTargetAddressSpace from ASTContext.

clang/test/CodeGenHLSL/nest_cbuf.hlsl
8 ↗(On Diff #448435)

is this generated as nested structs?

Add more test and comment.
Also use getTargetAddressSpace.

python3kgae marked 3 inline comments as done.Aug 8 2022, 3:39 PM
python3kgae added inline comments.
clang/lib/CodeGen/CGHLSLRuntime.cpp
65

Added comments.
Will check how union is supported in clang codeGen. The behavior feels similar, hope could share some code.

clang/test/CodeGenHLSL/nest_cbuf.hlsl
8 ↗(On Diff #448435)

No. This will generate as two separate structs.

beanz added inline comments.Aug 9 2022, 9:51 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
99

Why are you manually inserting padding?

IR level accesses don't require explicit layout, and we don't do this in DXC either.

beanz added a comment.Aug 12 2022, 5:30 PM

Now that I'm seeing the code in D131370, I don't know that this is the right way to do things.

I think using address spaces like this is odd. Address spaces aren't really intended for use differentiating high level access types, but rather memory regions and properties of that memory. I feel like the use of address spaces in this change and in D131370 just makes it more complicated and gets in the way.

In DXIL, since we don't really have raw memory load and stores, none of these address spaces mean anything. It is probably just cleaner to not use address spaces in the CodeGen here either.

Now that I'm seeing the code in D131370, I don't know that this is the right way to do things.

I think using address spaces like this is odd. Address spaces aren't really intended for use differentiating high level access types, but rather memory regions and properties of that memory. I feel like the use of address spaces in this change and in D131370 just makes it more complicated and gets in the way.

In DXIL, since we don't really have raw memory load and stores, none of these address spaces mean anything. It is probably just cleaner to not use address spaces in the CodeGen here either.

If not use address space, how do llvm passes know a cbuffer ptr is different from a tbuffer ptr or ptr to a static global variable? Or llvm passes don't need to know the difference?

clang/lib/CodeGen/CGHLSLRuntime.cpp
99

Does DL take care of the alignment

Remove address space and manual align.

python3kgae marked an inline comment as done.Sep 21 2022, 11:24 PM

Now that I'm seeing the code in D131370, I don't know that this is the right way to do things.

I think using address spaces like this is odd. Address spaces aren't really intended for use differentiating high level access types, but rather memory regions and properties of that memory. I feel like the use of address spaces in this change and in D131370 just makes it more complicated and gets in the way.

In DXIL, since we don't really have raw memory load and stores, none of these address spaces mean anything. It is probably just cleaner to not use address spaces in the CodeGen here either.

Removed address space.
Also removed nested HLSLBuffer and namespace inside HLSLBuffer.

Remove nested HLSLBuffer and namespace inside HLSLBuffer.

aaron.ballman added inline comments.Sep 22 2022, 5:02 AM
clang/lib/CodeGen/CGHLSLRuntime.cpp
82–84

Please spell out these types.

102–103

Please spell out these types.

109

Same here

148
150–153
177

Please spell out the type.

clang/lib/CodeGen/CGHLSLRuntime.h
51
76
python3kgae marked 8 inline comments as done.

Code cleanup to match comments

beanz added a comment.Sep 22 2022, 1:40 PM

This is looking great. Thank you for all your work iterating on this.

Please update the description to reflect the changes in implementation.

clang/lib/CodeGen/CGHLSLRuntime.cpp
121

Please remove the empty function. We can add it when it does something. Also make sure we have an issue for the FIXME.

138

Please file an issue for this or link to the issue if it exists.

python3kgae edited the summary of this revision. (Show Details)Sep 22 2022, 1:57 PM
python3kgae marked 2 inline comments as done.
python3kgae edited the summary of this revision. (Show Details)

Create issue for FIXME.

No additional comments from me, but I don't feel comfortable signing off on the codegen bits; adding some codegen code owners.

beanz added a reviewer: asl.Oct 4 2022, 2:46 PM
beanz added a subscriber: asl.

+@asl for codegen owner perspective.

This LGTM too. The changes here are well isolated to HLSL, so they should have no adverse impact on other language support.

@efriedma, @asl & @rjmccall any feedback?

efriedma added inline comments.Oct 12 2022, 4:46 PM
clang/lib/CodeGen/CGDecl.cpp
132

I'm a little confused by this. If it's possible to declare an HLSLBuffer inside a function, why don't you need to handle it? If it isn't possible to declare an HLSLBuffer this way, can you just move this to use the llvm_unreachable()?

clang/lib/CodeGen/CGHLSLRuntime.cpp
113

Messing with globals like this feels a little weird, but I guess it's fine if nothing actually tries to use the erased globals after this code runs. I'm a little concerned that someone might accidentally rearrange the relevant code in the future (CodeGenModule has a bunch of maps which aren't cleared before this code runs).

python3kgae marked an inline comment as done.

Go llvm_unreachable is hit HLSLBuffer inside function.

python3kgae marked an inline comment as done.Oct 12 2022, 5:37 PM
python3kgae added inline comments.
clang/lib/CodeGen/CGDecl.cpp
132

Nice catch.
Fixed.

clang/lib/CodeGen/CGHLSLRuntime.cpp
113

These globals should not be in any map except Buf.Constants which is used here.
If another map has these globals, we should remove them from the map.
Cannot see any other map has these globals now.

efriedma accepted this revision.Oct 12 2022, 6:13 PM
efriedma added inline comments.
clang/lib/CodeGen/CGHLSLRuntime.cpp
113

Oh, I see. Probably not an issue in that case.

This revision is now accepted and ready to land.Oct 12 2022, 6:13 PM
This revision was landed with ongoing or failed builds.Oct 12 2022, 9:17 PM
This revision was automatically updated to reflect the committed changes.
python3kgae marked an inline comment as done.