This is an archive of the discontinued LLVM Phabricator instance.

[clang] Replace use of Type::getPointerTo() (NFC)
ClosedPublic

Authored by JOE1994 on Jun 6 2023, 4:40 PM.

Details

Summary

Partial progress towards replacing in-tree uses of Type::getPointerTo().
This needs to be done before deprecating the API.

Diff Detail

Event Timeline

JOE1994 created this revision.Jun 6 2023, 4:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 4:40 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
JOE1994 requested review of this revision.Jun 6 2023, 4:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 4:40 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
nikic added inline comments.Jun 7 2023, 6:13 AM
clang/lib/CodeGen/Address.h
135 ↗(On Diff #529089)

In these cases, what we want to do is remove the BitCast, which is no longer necessary.

JOE1994 updated this revision to Diff 529317.Jun 7 2023, 8:29 AM

If getPointerTo() is used just to support an unnecessary bitcast, just get rid of the bitcast.

JOE1994 added inline comments.Jun 7 2023, 8:41 AM
clang/lib/CodeGen/Address.h
135 ↗(On Diff #529089)

Thank you for your feedback! I've updated the revision accordingly.

Some tests that run with -no-opaque-pointers began failing after I updated the revision to get rid of bitcasts.
The bitcasts are still needed if running Clang with -no-opaque-pointers.

nikic added a comment.Jun 8 2023, 9:06 AM

Some tests that run with -no-opaque-pointers began failing after I updated the revision to get rid of bitcasts.
The bitcasts are still needed if running Clang with -no-opaque-pointers.

I have finished migrating these tests and removed the flag in https://reviews.llvm.org/D152447, so this should no longer be an issue.

clang/lib/CodeGen/CGBuilder.h
161–162

Can remove this bit cast.

clang/lib/CodeGen/CGBuiltin.cpp
222–223

We should be using the overload that take as a context rather than a type, to create an opaque pointers. The variable name should also drop the mention of the specific type.

Same for other uses.

324

Remove bitcast

415–416

Remove bitcast

4658

Remove bitcast (more below...)

JOE1994 added inline comments.Jun 8 2023, 9:58 AM
clang/lib/CodeGen/CGBuilder.h
161–162

Wouldn't removing the bitcast cause behavior change for uses of CreateElementBitCast that supply a Name that is not ""?

nikic added inline comments.Jun 8 2023, 11:38 AM
clang/lib/CodeGen/CGBuilder.h
161–162

This will never actually create an instruction, so the name is already ignored now. It would make sense to remove the parameter altogether.

JOE1994 updated this revision to Diff 529976.Jun 9 2023, 8:31 AM
  • Update uses of PointerType::get(Ty) & PointerType::getUnqual to be in overloaded form of PointerType::get that takes LLVMContext&
  • Remove more unnecessary bitcasts which were previously overlooked.
  • Remove pointee type info from Type variable names
JOE1994 added inline comments.Jun 9 2023, 8:42 AM
clang/lib/CodeGen/CGBuilder.h
161–162

Thank you for the clarification.

I've updated the revision to get rid of the bitcast.

I'll prepare a separate commit that gets rid of the Name parameter.

Please use clang-format on the modified lines.

clang/lib/CodeGen/CGBuilder.h
161–162

The argument can be removed.

Idea for a follow-up: I would also consider removing this method because it does not do what its name says.
Maybe replace it with Address::withElementType analagous to Address::withPointer / Address::withAlignment?

clang/lib/CodeGen/CGBuiltin.cpp
9349–9350

Braces are now redundant.

clang/lib/CodeGen/CGCUDANV.cpp
238–240

These are all the same types. Replace the variables with single PtrTy?

271–272

Pass Context

538–542

Pass Context

clang/lib/CodeGen/CGCXX.cpp
177

This looks wrong. It used to be GetFunctionType(TargetDecl).

185–186

What's the reason for this change?

clang/lib/CodeGen/CGException.cpp
2120

I guess this was intended to be named RecordTy.

clang/lib/CodeGen/CGExprConstant.cpp
1944–1946
clang/lib/CodeGen/CGObjCRuntime.cpp
374–375

Pass context here

384–385

Pass context here.
Can also be moved above if.

clang/lib/CodeGen/ItaniumCXXABI.cpp
816–818
1925–1928
2551–2554

I think this can be simplified further to just:
llvm::Type *AddrPtrTy = addr->getType();

JOE1994 updated this revision to Diff 531593.Jun 14 2023, 7:28 PM
  • Apply suggestions from barannikov88
  • Used git clang-format to tidy up code format

Thank you for your feedback!

JOE1994 added inline comments.Jun 14 2023, 7:30 PM
clang/lib/CodeGen/CGBuilder.h
161–162

Thank you for the suggestion.

I have a follow-up reivision for removing the Name parameter. (https://reviews.llvm.org/D152551)

After this revision gets merged, I can update the follow-up revision to replace CreateElementBitCast also.

clang/lib/CodeGen/CGCUDANV.cpp
238–240

Thank you for the suggestion.

Unifying them to a single PtrTy adds a lot of diff,
so I think it deserves to be in a separate standalone commit.
(I can create a follow-up revision after this gets merged).

clang/lib/CodeGen/CGCXX.cpp
177

I've simply moved the line defining AliasValueType closer to its use.
I think AliasValueType is right to be initialized with getFunctionType(AliasDecl).

Moving the line to below isn't necessary, and it's rather causing confusion.
I'll move it back to where it was.

185–186

Since AliasType got removed above, the assert condition needs to be updated one way or another.

The assert was written in 2010 (before opaque pointers),
and it's checking equality of two pointer types (which was equivalent to checking equality of pointee type & address space).

With opaque pointers enabled, I thought the assert needs to be updated to check equality of both the pointee types & address spaces separately in order to preserve the original intent.

barannikov88 accepted this revision.Jun 14 2023, 9:01 PM

LGTM with CI fixed, thanks.
I'd like @nikic to also take a look though.

This revision is now accepted and ready to land.Jun 14 2023, 9:01 PM

I guess you need to rebase onto 066fb7a5 at least.

nikic added a comment.Jun 15 2023, 1:17 AM

Looks reasonable. Main note I have is that I think the use of getUnqual() over passing AddrSpace=0 would be preferred.

clang/lib/CodeGen/CGAtomic.cpp
91–92

getUnqual

clang/lib/CodeGen/TargetInfo.cpp
2057

getLLVMContext?

JOE1994 updated this revision to Diff 531889.Jun 15 2023, 1:15 PM
  • Use llvm::PointerType::getUnqual whenever possible
JOE1994 added inline comments.Jun 15 2023, 1:19 PM
clang/lib/CodeGen/TargetInfo.cpp
2057

getLLVMContext doesn't seem available in this context.

Below is the build error message I get when using getLLVMContext.

~/llvm-project/clang/lib/CodeGen/TargetInfo.cpp: In member function 'void {anonymous}::X86_32ABIInfo::addFieldToArgStruct(llvm::SmallVector<llvm::Type*, 6>&, clang::CharUnits&, clang::CodeGen::ABIArgInfo&, clang::QualType) const':
~/llvm-project/clang/lib/CodeGen/TargetInfo.cpp:2057:41: error: 'getLLVMContext' was not declared in this scope; did you mean 'getVMContext'?

On line 2066, getVMContext is used when a LLVMContext& is needed.
So I just followed it to use getVMContext on line 2057.

nikic accepted this revision.Jun 15 2023, 1:23 PM

LGTM -- do you have commit access, or should someone commit this for you?

I do not have commit access yet.
Could one of the reviewers land this patch for me?
Please use "Youngsuk Kim <youngsuk.kim@hpe.com>" to commit the change. Thank you!

barannikov88 requested changes to this revision.EditedJun 15 2023, 8:44 PM

@JOE1994
Please run the testsuite locally (ninja check-clang), there is at least one test that crashes (CodeGen/constructor-attribute.c).

This revision now requires changes to proceed.Jun 15 2023, 8:44 PM
barannikov88 added inline comments.Jun 15 2023, 8:56 PM
clang/lib/CodeGen/ItaniumCXXABI.cpp
2551–2554

I was wrong, addr can be null here. Sorry.

JOE1994 updated this revision to Diff 532164.Jun 16 2023, 8:22 AM
  • Fix nullptr bug (fixes Clang regression test : Clang :: CodeGen/constructor-attribute.c )
JOE1994 added inline comments.Jun 16 2023, 8:24 AM
clang/lib/CodeGen/ItaniumCXXABI.cpp
2551–2554

Thanks for finding this!

Updating code to handle the nullptr case fixes the Clang regression test Clang :: CodeGen/constructor-attribute.c.

With the updated revision, I don't see new test failures from ninja check-clang & ninja check-llvm & ninja check-clang-unit & ninja check-llvm-unit .

With the updated revision, I don't see new test failures from ninja check-clang & ninja check-llvm & ninja check-clang-unit & ninja check-llvm-unit .

Thank you, it was my bad.
I'll land this later today.

barannikov88 accepted this revision.Jun 16 2023, 12:05 PM
This revision is now accepted and ready to land.Jun 16 2023, 12:05 PM
This revision was automatically updated to reflect the committed changes.