Skip to content

[WIP] Introduce a G_PTRTOADDR GIsel node #140300

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

Draft
wants to merge 2 commits into
base: users/arichardson/spr/main.wip-introduce-a-g_ptrtoaddr-gisel-node
Choose a base branch
from

Conversation

arichardson
Copy link
Member

No description provided.

Created using spr 1.3.6-beta.1
Copy link

github-actions bot commented May 16, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- llvm/include/llvm/CodeGen/GlobalISel/GenericMachineInstrs.h llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp llvm/lib/CodeGen/GlobalISel/LegacyLegalizerInfo.cpp llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp llvm/lib/CodeGen/MachineVerifier.cpp
View the diff from clang-format here.
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h b/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h
index 3f465b824..722e3e61e 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h
@@ -486,7 +486,7 @@ private:
   bool translatePtrToInt(const User &U, MachineIRBuilder &MIRBuilder) {
     return translateCast(TargetOpcode::G_PTRTOINT, U, MIRBuilder);
   }
-  bool translatePtrToAddr(const User &U, MachineIRBuilder &MIRBuilder)  {
+  bool translatePtrToAddr(const User &U, MachineIRBuilder &MIRBuilder) {
     return translateCast(TargetOpcode::G_PTRTOADDR, U, MIRBuilder);
   }
   bool translateTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 39fc20011..ca1da0138 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -4695,7 +4695,7 @@ LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT LowerHintTy) {
     return lowerTRUNC(MI);
   case G_PTRTOADDR:
     return lowerPTRTOADDR(MI);
-  GISEL_VECREDUCE_CASES_NONSEQ
+    GISEL_VECREDUCE_CASES_NONSEQ
     return lowerVectorReduction(MI);
   case G_VAARG:
     return lowerVAArg(MI);

Created using spr 1.3.6-beta.1
@@ -204,6 +207,10 @@ LegacyLegalizerInfo::getAspectAction(const InstrAspect &Aspect) const {
if (Aspect.Type.isScalar() || Aspect.Type.isPointer())
return findScalarLegalAction(Aspect);
assert(Aspect.Type.isVector());
if (Aspect.Opcode == TargetOpcode::G_PTRTOADDR) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is really ugly and I don't know if there is a better solution?

@arichardson
Copy link
Member Author

GISelValueTracking correctly implemented in #143816 and #143815

Comment on lines +7422 to +7423
auto DstReg = MI.getOperand(0).getReg();
auto SrcReg = MI.getOperand(1).getReg();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto DstReg = MI.getOperand(0).getReg();
auto SrcReg = MI.getOperand(1).getReg();
Register DstReg = MI.getOperand(0).getReg();
Register SrcReg = MI.getOperand(1).getReg();

Comment on lines +7434 to +7438
auto PtrToInt = MIRBuilder.buildPtrToInt(IntPtrTy, SrcReg);
auto Addr = PtrToInt;
if (AddrTy != IntPtrTy)
Addr = MIRBuilder.buildTrunc(AddrTy, PtrToInt.getReg(0));
MIRBuilder.buildZExtOrTrunc(DstReg, Addr.getReg(0));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto PtrToInt = MIRBuilder.buildPtrToInt(IntPtrTy, SrcReg);
auto Addr = PtrToInt;
if (AddrTy != IntPtrTy)
Addr = MIRBuilder.buildTrunc(AddrTy, PtrToInt.getReg(0));
MIRBuilder.buildZExtOrTrunc(DstReg, Addr.getReg(0));
Register Addr = MIRBuilder.buildPtrToInt(IntPtrTy, SrcReg).getReg(0);
if (AddrTy != IntPtrTy)
Addr = MIRBuilder.buildTrunc(AddrTy, Addr).getReg(0);
MIRBuilder.buildZExtOrTrunc(DstReg, Addr);

@@ -7407,6 +7412,34 @@ LegalizerHelper::LegalizeResult LegalizerHelper::lowerTRUNC(MachineInstr &MI) {
return UnableToLegalize;
}

LegalizerHelper::LegalizeResult
LegalizerHelper::lowerPTRTOADDR(MachineInstr &MI) {
// Lower G_PTRTOADDR as a truncate to address width of G_PTROINT and then
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lower G_PTRTOADDR as a truncate to address width of G_PTROINT and then
// Lower G_PTRTOADDR as a truncate to address width of G_PTRTOINT and then

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