Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CIRGen][TBAA] Add support for scalar types #1220

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

PikachuHyA
Copy link
Collaborator

No description provided.

@PikachuHyA
Copy link
Collaborator Author

follow #1076 (review)

This patch adds support for TBAA with scalar types. It retrieves type names in the TBAAPass, utilizing as much information from the CIR as possible, rather than duplicating the original codegen implementation in CIRGenTBAA.

A limitation of this patch is that it cannot handle long long types. Instead, long long is treated as long. Specifically, long long is represented as s64i in CIR, and I have mapped s64i to the long type.

@bcardosolopes bcardosolopes changed the title [CIR][CIRGen][TBAA] Add support for TBAA with scalar types [CIR][CIRGen][TBAA] Add support for scalar types Dec 10, 2024
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Outdated Show resolved Hide resolved
@PikachuHyA
Copy link
Collaborator Author

I have update the patch. please review again.

  • add NYI if no tests covered
  • add tests (tbaa-struct.cpp and tbaa-vtpr.cpp)
  • format tablegen file

A limitation of this patch is that it cannot handle long long types. Instead, long long is treated as long. Specifically, long long is represented as s64i in CIR, and I have mapped s64i to the long type.

I would appreciate your thoughts on this limitation.

clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
clang/test/CIR/CodeGen/tbaa-struct.cpp Show resolved Hide resolved
// Pointee values may have incomplete types, but they shall never be
// dereferenced.
if (accessType->isIncompleteType()) {
llvm_unreachable("NYI");
Copy link
Member

Choose a reason for hiding this comment

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

Our internal builds use -O2 to build our apps, and seems like this path and some of the others will always run under -O2. For paths that will unconditionally run, you are better off with, e.g. assert(!MissingFeature::tbaaIncompleteType()) or something. Can you confirm that all added unrecheables are never executed just yet? An alternative is to add a temporary cc1 flag and only enable tbaa when the flag is used, once you are done with the full work you would remove the flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Our internal builds use -O2 to build our apps, and seems like this path and some of the others will always run under -O2

did not encounter the NYI issue when run with -O2. it may be related to the struct type.

I think adding MissingFeatures::tbaaIncompleteType() would be a good idea.

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
@bcardosolopes
Copy link
Member

I would appreciate your thoughts on this limitation.

Haven't had the time to dig just yet, we can look at that once this is closer to be merged!

@PikachuHyA
Copy link
Collaborator Author

Changes Summary

  • rename CIR_TBAAScalarTypeDescriptorAttr to CIR_TBAAScalarAttr
  • change the type of tbaa parameter from ArrayAttr to CIR_AnyTBAAAttr
  • remove TBAAPass and lowering CIR_TBAAScalarAttr in ConvertCIRToLLVMPass
  • add MissingFeatures::tbaaVTablePtr and MissingFeatures::tbaaIncompleteType

@PikachuHyA
Copy link
Collaborator Author

I noticed the LowerXXX classes (e.g., LowerModule, LowerTypes) in clang/lib/CIR/Dialect/Transforms/TargetLowering.

How about adding a LowerTBAA class to encapsulate TBAA lowering-related functions (e.g., getChar, lowerScalarType) and state management (e.g., some struct cache)?

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Just a few more nits and this looks ready!

clang/include/clang/CIR/Dialect/IR/CIROps.td Outdated Show resolved Hide resolved
clang/lib/CIR/CodeGen/CIRGenTBAA.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
@bcardosolopes
Copy link
Member

I noticed the LowerXXX classes (e.g., LowerModule, LowerTypes) in clang/lib/CIR/Dialect/Transforms/TargetLowering.

If structs change as part of ABI lowering or new ones get created, we'll need to update information accordingly, but I think those can be handled directly as part of LowerTypes and whatnots.

How about adding a LowerTBAA class to encapsulate TBAA lowering-related functions (e.g., getChar, lowerScalarType)

LowerXXX is CIR to CIR. It seems to me that until LLVM lowering, we don't need any of those, since they are LLVM specific.

and state management (e.g., some struct cache)?

Attributes are value semantic, so MLIR should cache things for us. Do you have an example of something that might escape that?

@PikachuHyA
Copy link
Collaborator Author

Attributes are value semantic, so MLIR should cache things for us. Do you have an example of something that might escape that?

Let's discuss the scenario where I have submitted patch support for struct types.
For scalar types, we can disregard it.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for your patience!

For long long, can you create an issue describing the problem and we can discuss it there? My feeling is that it will need some type changes in CIR to accommodate and will require it's own PR anyways.

@bcardosolopes bcardosolopes merged commit c94c04e into llvm:main Dec 18, 2024
6 checks passed
@PikachuHyA
Copy link
Collaborator Author

For long long, can you create an issue describing the problem and we can discuss it there? My feeling is that it will need some type changes in CIR to accommodate and will require it's own PR anyways.

added #1241

bcardosolopes pushed a commit that referenced this pull request Dec 19, 2024
)

This patch follows
#1220 (comment) by
augmenting `CIR_Type` with a new field, `tbaaName`. Specifically, it
enables TableGen support for the `-gen-cir-tbaa-name-lowering` option,
allowing for the generation of `getTBAAName` functions based on the
`tbaaName`. This enhancement enables us to replace the hardcoded TBAA
names in the `getTypeName` function with the newly generated
`getTBAAName`.
bcardosolopes added a commit that referenced this pull request Dec 20, 2024
bcardosolopes added a commit that referenced this pull request Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants