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] Enable address space flags and re-categorize / fix usages #614

Closed
wants to merge 5 commits into from

Conversation

seven-mile
Copy link
Collaborator

Depend on #606 , will rebase after its merging.

This PR fixes simple cases and re-categorize other cases to addressSpaceInGlobalVar or addressSpaceCasting. Maybe the changes are hard to review😂. But I will solve them in months, so no need to be strict with the precise categorization.

@seven-mile seven-mile marked this pull request as ready for review May 16, 2024 18:49
Copy link
Collaborator

@orbiri orbiri left a comment

Choose a reason for hiding this comment

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

Awesome PR! :D

Missing tests to cover the new codegen paths you have enabled, please make sure you cover them!

@@ -128,7 +128,6 @@ struct CIRGenTypeCache {
// Address spaces are not yet fully supported, but the usage of the default
// alloca address space can be used for now only for comparison with the
// default address space.
assert(!UnimplementedFeature::addressSpace());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe you should switch this with addressSpaceCasting :)

Comment on lines +846 to +847
LangAS AAS = getASTAllocaAddressSpace();
auto TargetAAS = CGM.getASTContext().getTargetAddressSpace(AAS);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clang's code sets the alloca's address space to the one described by the data-layout, and here you are using another path in the codegen - the targetCIRGenInfo/targetCodeGenInfo path. Not sure what's the right choice here, need to dive into a rabbit hole to understand the original intention of use for both.

In general, we are trying to stick as much as possible to clang's codegen structure to avoid re-making past mistakes :D

I'll note that it do seem like I missed this details in the alloca builder I implemented a while ago! I guess we should either understand how to pass the datalayout here or add some nice TODO comment for future us :)

Copy link
Collaborator Author

@seven-mile seven-mile May 17, 2024

Choose a reason for hiding this comment

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

For the two methods to get the alloca addrspace, I think these lines of comment helps. I noticed that the code is creating alloca and cast it immediately, therefore I naively simplify it to a direct alloca😂. Sorry for the mistake.

// An alloca will always return a pointer to the alloca (stack) address
// space. This address space need not be the same as the AST / Language
// default (e.g. in C / C++ auto vars are in the generic address space). At
// the AST level this is handled within CreateTempAlloca et al., but for the
// builtin / dynamic alloca we have to handle it here. We use an explicit cast
// instead of passing an AS to CreateAlloca so as to not inhibit optimisation.
case Builtin::BIalloca:

Most of targets in current codebase indeed implement getASTAllocaAddressSpace with DataLayout internally. The intention here should be to ensure the generality. (I don't fully understand the case of automatic variables mentioned in the comment above.)

Anyway, shall we nicely have a addressSpaceCasting() mark here? For reference DataLayout directly, I know CIR uses the MLIR model, which has getAllocaMemorySpace method. It's not public through CIRDataLayout though, probably skipped at that time. This should be a workable path.😉

@@ -1206,23 +1206,21 @@ RValue CIRGenFunction::buildCall(clang::QualType CalleeType,
// some trivial cases].
// That is, in the general case, we should assume that a call through an
// unprototyped function type works like a *non-variadic* call. The way we
// make this work is to cast to the exxact type fo the promoted arguments.
// make this work is to cast to the exact type fo the promoted arguments.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nittiest of picks - if you are already here:

Suggested change
// make this work is to cast to the exact type fo the promoted arguments.
// make this work is to cast to the exact type of the promoted arguments.

Comment on lines +1223 to 1224
assert(!UnimplementedFeature::addressSpaceInGlobalVar());
Addr = builder.create<mlir::cir::GetGlobalOp>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it would make sense to add an assert here that the address space is the default (0)? Future users may try to understand why some verifier fail downstream from the codegen, and it would be hard to trace it back to this very low-level path in the code :)

Comment on lines +1236 to +1237
auto CalleePtrTy = mlir::cir::PointerType::get(builder.getContext(),
CalleeTy, CalleePtrAS);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think you can hide the mlir::cir::PointerType::get(builder.getContext(), part in a CIRGenBuilder (or CIRBaseBuilder) method with a name along getPointerTy?

Can you use getPointerTo (from CIRBaseBuilder) instead? Hides some of the horrors ;)

@seven-mile
Copy link
Collaborator Author

Many thanks for reviewing this mess😂! I also found a bunch of extra usages to mark. I shall go through entirely.

For the test cases, I would better split this PR into two parts, one for NFC flags, another for fix with tests, making it tidier.

@seven-mile seven-mile closed this May 17, 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