Skip to content

[AMDGPU] Serialize WWM_REG vreg flag #110229

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

Merged

Conversation

optimisan
Copy link
Contributor

No description provided.

Copy link
Contributor Author

optimisan commented Sep 27, 2024

@optimisan optimisan marked this pull request as ready for review September 27, 2024 11:12
@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2024

@llvm/pr-subscribers-backend-amdgpu

Author: Akshat Oke (Akshat-Oke)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/110229.diff

5 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp (+15)
  • (modified) llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h (+2-2)
  • (modified) llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp (+11)
  • (modified) llvm/lib/Target/AMDGPU/SIRegisterInfo.h (+10)
  • (added) llvm/test/CodeGen/AMDGPU/virtual-registers.mir (+16)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index abd50748f2cc05..c42b443230acc9 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -1628,6 +1628,21 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
     MFI->reserveWWMRegister(ParsedReg);
   }
 
+  auto setRegisterFlags = [&](const VRegInfo &Info) {
+    for (const auto &Flag : Info.Flags) {
+      MFI->setFlag(Info.VReg, Flag);
+    }
+  };
+
+  for (const auto &P : PFS.VRegInfosNamed) {
+    const VRegInfo &Info = *P.second;
+    setRegisterFlags(Info);
+  }
+  for (const auto &P : PFS.VRegInfos) {
+    const VRegInfo &Info = *P.second;
+    setRegisterFlags(Info);
+  }
+
   auto parseAndCheckArgument = [&](const std::optional<yaml::SIArgument> &A,
                                    const TargetRegisterClass &RC,
                                    ArgDescriptor &Arg, unsigned UserSGPRs,
diff --git a/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h b/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h
index aff0b34947d688..7fffc706c4b027 100644
--- a/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h
+++ b/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h
@@ -684,8 +684,8 @@ class SIMachineFunctionInfo final : public AMDGPUMachineFunction,
 
   void setFlag(Register Reg, uint8_t Flag) {
     assert(Reg.isVirtual());
-    if (VRegFlags.inBounds(Reg))
-      VRegFlags[Reg] |= Flag;
+    VRegFlags.grow(Reg);
+    VRegFlags[Reg] |= Flag;
   }
 
   bool checkFlag(Register Reg, uint8_t Flag) const {
diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
index 2d1cd1bda3afe1..ad3e592127de80 100644
--- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
@@ -3614,3 +3614,14 @@ SIRegisterInfo::getSubRegAlignmentNumBits(const TargetRegisterClass *RC,
   }
   return 0;
 }
+
+SmallVector<std::string>
+SIRegisterInfo::getVRegFlagsOfReg(Register Reg,
+                                  const MachineFunction &MF) const {
+  SmallVector<std::string> RegFlags;
+  const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
+  if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) {
+    RegFlags.push_back("WWM_REG");
+  }
+  return RegFlags;
+}
diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.h b/llvm/lib/Target/AMDGPU/SIRegisterInfo.h
index 88d5686720985e..bce5a2aa792bd3 100644
--- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.h
+++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.h
@@ -449,6 +449,16 @@ class SIRegisterInfo final : public AMDGPUGenRegisterInfo {
   // No check if the subreg is supported by the current RC is made.
   unsigned getSubRegAlignmentNumBits(const TargetRegisterClass *RC,
                                      unsigned SubReg) const;
+
+  std::pair<bool, uint8_t> getVRegFlagValue(StringRef Name) const override {
+    if (Name == "WWM_REG") {
+      return {true, AMDGPU::VirtRegFlag::WWM_REG};
+    }
+    return {false, 0};
+  }
+
+  SmallVector<std::string>
+  getVRegFlagsOfReg(Register Reg, const MachineFunction &MF) const override;
 };
 
 namespace AMDGPU {
diff --git a/llvm/test/CodeGen/AMDGPU/virtual-registers.mir b/llvm/test/CodeGen/AMDGPU/virtual-registers.mir
new file mode 100644
index 00000000000000..3ea8f6eafcf10c
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/virtual-registers.mir
@@ -0,0 +1,16 @@
+# RUN: llc -mtriple=amdgcn -run-pass=none -o - %s | FileCheck %s
+# This test ensures that the MIR parser parses virtual register flags correctly
+
+---
+name: vregs
+# CHECK: registers:
+# CHECK-NEXT:   - { id: 0, class: vgpr_32, preferred-register: '$vgpr1', flags: [ WWM_REG ] }
+# CHECK-NEXT:   - { id: 1, class: sgpr_64, preferred-register: '$sgpr0_sgpr1', flags: [  ] }
+# CHECK-NEXT:   - { id: 2, class: sgpr_64, preferred-register: '', flags: [  ] }
+registers:
+  - { id: 0, class: vgpr_32, preferred-register: $vgpr1, flags: [ WWM_REG ]}
+  - { id: 1, class: sgpr_64, preferred-register: $sgpr0_sgpr1 }
+body: |
+  bb.0:
+    %2:sgpr_64 = COPY %1
+    %1:sgpr_64 = COPY %0

@@ -684,8 +684,8 @@ class SIMachineFunctionInfo final : public AMDGPUMachineFunction,

void setFlag(Register Reg, uint8_t Flag) {
assert(Reg.isVirtual());
if (VRegFlags.inBounds(Reg))
VRegFlags[Reg] |= Flag;
VRegFlags.grow(Reg);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess, this change is unnecessary. Keep the inbounds check back.
The following change in the other patch would make sure to grow VRegFlags for each virtual register it encountered.
https://github.com/llvm/llvm-project/pull/110228/files#diff-c72079b2a595aca3300d5e3c15d227f81937f2745f7c5494fcf1fe9ba37d8828R1789

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The MIR function is parsed after parsing the options, so the noteNewVirtualRegister callback doesn't take effect.

Copy link
Contributor

Choose a reason for hiding this comment

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

You changed this though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't take effect here.
Might be better to move it to a separate commit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This change doesn't make sense to me. What will happen to the regular flow when it reaches from MRI createVirtualRegister? Isn't duplicating the size?
https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/MachineRegisterInfo.cpp#L166

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Register info is an avenue for noting new virtual registers so I've put it here #111634

@@ -0,0 +1,16 @@
# RUN: llc -mtriple=amdgcn -run-pass=none -o - %s | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mostly, the tests related to serialize options go in llvm/test/CodeGen/MIR/AMDGPU/machine-function-info-no-ir.mir.
Additionally, a negative test is required for this serialized option and that should be a separate test like llvm/test/CodeGen/MIR/AMDGPU/sgpr-for-exec-copy-invalid-reg.mir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Negative test is now in MIR/Generic.

@@ -1628,6 +1628,21 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
MFI->reserveWWMRegister(ParsedReg);
}

auto setRegisterFlags = [&](const VRegInfo &Info) {
for (const auto &Flag : Info.Flags) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No auto, no reference. This is just uint8_t

Comment on lines 1637 to 1722
for (const auto &P : PFS.VRegInfosNamed) {
const VRegInfo &Info = *P.second;
Copy link
Contributor

Choose a reason for hiding this comment

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

c++17 destructuring

Comment on lines 1641 to 1726
for (const auto &P : PFS.VRegInfos) {
const VRegInfo &Info = *P.second;
Copy link
Contributor

Choose a reason for hiding this comment

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

c++17 destructuring

@@ -3614,3 +3614,14 @@ SIRegisterInfo::getSubRegAlignmentNumBits(const TargetRegisterClass *RC,
}
return 0;
}

SmallVector<std::string>
Copy link
Contributor

Choose a reason for hiding this comment

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

probably should just be const char*, this will probably only ever be used with literals

@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_mir_serialize_virtual_register_flags branch from f494d56 to 336b9bc Compare October 4, 2024 06:40
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 35536a8 to c428da0 Compare October 4, 2024 06:40
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from c428da0 to 6651bfa Compare October 8, 2024 06:31
Comment on lines 3848 to 3862
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) {
RegFlags.push_back(SmallString<8>("WWM_REG"));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) {
RegFlags.push_back(SmallString<8>("WWM_REG"));
}
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG))
RegFlags.push_back("WWM_REG");

I would hope construct from literal works fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I change this to a const char* instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I expect only literals, so could use StringRef or StringLiteral

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, will keep this.
I have to keep the explicit construct since "WWM_REG" is being refused to be casted to a SmallString (without creating a StringRef explicitly)

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the interface could change from SmallString to StringLiteral

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it

Comment on lines 459 to 462
if (Name == "WWM_REG") {
return AMDGPU::VirtRegFlag::WWM_REG;
}
return {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (Name == "WWM_REG") {
return AMDGPU::VirtRegFlag::WWM_REG;
}
return {};
return Name == "WWM_REG" ? AMDGPU::VirtRegFlag::WWM_REG : {};

@@ -684,8 +684,8 @@ class SIMachineFunctionInfo final : public AMDGPUMachineFunction,

void setFlag(Register Reg, uint8_t Flag) {
assert(Reg.isVirtual());
if (VRegFlags.inBounds(Reg))
VRegFlags[Reg] |= Flag;
VRegFlags.grow(Reg);
Copy link
Contributor

Choose a reason for hiding this comment

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

You changed this though?

Comment on lines 459 to 460
return (Name == "WWM_REG") ? AMDGPU::VirtRegFlag::WWM_REG
: std::optional<uint8_t>{};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return (Name == "WWM_REG") ? AMDGPU::VirtRegFlag::WWM_REG
: std::optional<uint8_t>{};
return Name == "WWM_REG" ? AMDGPU::VirtRegFlag::WWM_REG
: std::optional<uint8_t>{};

@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_mir_serialize_virtual_register_flags branch from 9f8b6eb to 7af9e5e Compare October 9, 2024 05:57
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 81baf9e to 79de29b Compare October 9, 2024 05:57
@optimisan optimisan changed the base branch from users/Akshat-Oke/09-27-_mir_serialize_virtual_register_flags to users/Akshat-Oke/10-09-_mir_add_missing_notenewvirtualregister_callbacks October 9, 2024 05:58
@optimisan optimisan force-pushed the users/Akshat-Oke/10-09-_mir_add_missing_notenewvirtualregister_callbacks branch from 2b87714 to 8e7f366 Compare October 9, 2024 06:16
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 79de29b to 81059dc Compare October 9, 2024 06:16
Comment on lines 1720 to 1722
for (uint8_t Flag : Info.Flags) {
MFI->setFlag(Info.VReg, Flag);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might as well just inline this and duplicate the code, the loop is trivial enough

Comment on lines 3860 to 3862
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) {
RegFlags.push_back("WWM_REG");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) {
RegFlags.push_back("WWM_REG");
}
if (FuncInfo->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG))
RegFlags.push_back("WWM_REG");

@optimisan optimisan force-pushed the users/Akshat-Oke/10-09-_mir_add_missing_notenewvirtualregister_callbacks branch from bf5cc39 to 9dd1158 Compare October 14, 2024 08:21
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 81059dc to db80f0b Compare October 14, 2024 08:21
@optimisan optimisan force-pushed the users/Akshat-Oke/10-09-_mir_add_missing_notenewvirtualregister_callbacks branch from 9dd1158 to 9390926 Compare October 14, 2024 08:31
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from db80f0b to 3240cf9 Compare October 14, 2024 08:31
@optimisan optimisan force-pushed the users/Akshat-Oke/10-09-_mir_add_missing_notenewvirtualregister_callbacks branch from 9390926 to 38a9c7b Compare October 14, 2024 08:51
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 3240cf9 to 4870a8a Compare October 14, 2024 08:52
@optimisan optimisan force-pushed the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch from 32b44f5 to 0712903 Compare October 14, 2024 09:03
@optimisan optimisan merged commit bec839d into main Oct 14, 2024
5 of 7 checks passed
@optimisan optimisan deleted the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch October 14, 2024 09:07
@pcc
Copy link
Contributor

pcc commented Oct 14, 2024

It looks like this change caused LSan reports. See e.g. https://lab.llvm.org/buildbot/#/builders/52/builds/2928

Can you please take a look?

@optimisan
Copy link
Contributor Author

optimisan commented Oct 15, 2024

Taking a look

pcc added a commit that referenced this pull request Oct 15, 2024
@pcc
Copy link
Contributor

pcc commented Oct 15, 2024

I reverted the change for now, please reland when you have a fix.

@optimisan
Copy link
Contributor Author

optimisan commented Oct 16, 2024

Yep
Have a fix here #112479

@optimisan optimisan restored the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch October 16, 2024 07:22
@optimisan optimisan deleted the users/Akshat-Oke/09-27-_amdgpu_serialize_wwm_reg_vreg_flag branch October 16, 2024 07:42
optimisan added a commit to optimisan/llvm-project that referenced this pull request Oct 16, 2024
optimisan added a commit to optimisan/llvm-project that referenced this pull request Oct 16, 2024
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
optimisan added a commit to optimisan/llvm-project that referenced this pull request Oct 21, 2024
optimisan added a commit that referenced this pull request Oct 21, 2024
A reland but not an exact copy as `VRegInfo.Flags` from the parser is
now an int8 instead of a vector; so only need to copy over the value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants