Skip to content

[IPSCCP][FuncSpec] Protect against metadata access from call args. #124284

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
merged 1 commit into from
Jan 25, 2025

Conversation

davemgreen
Copy link
Collaborator

Fixes an issue reported from #114964, where metadata arguments were looked at to consider constants.

@llvmbot
Copy link
Member

llvmbot commented Jan 24, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-function-specialization

Author: David Green (davemgreen)

Changes

Fixes an issue reported from #114964, where metadata arguments were looked at to consider constants.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/FunctionSpecialization.cpp (+2)
  • (added) llvm/test/Transforms/FunctionSpecialization/solver-constant-strictfpmetadata.ll (+17)
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 449d64d1614ff1..fa01156315741d 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -415,6 +415,8 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
 
   for (unsigned Idx = 0, E = I.getNumOperands() - 1; Idx != E; ++Idx) {
     Value *V = I.getOperand(Idx);
+    if (isa<MetadataAsValue>(V))
+      continue;
     Constant *C = findConstantFor(V);
     if (!C)
       return nullptr;
diff --git a/llvm/test/Transforms/FunctionSpecialization/solver-constant-strictfpmetadata.ll b/llvm/test/Transforms/FunctionSpecialization/solver-constant-strictfpmetadata.ll
new file mode 100644
index 00000000000000..99224b4efba6bc
--- /dev/null
+++ b/llvm/test/Transforms/FunctionSpecialization/solver-constant-strictfpmetadata.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=ipsccp -force-specialization -S < %s | FileCheck %s
+
+define float @test(ptr %this, float %cm, i1 %0) strictfp {
+; CHECK-LABEL: define float @test(
+; CHECK-SAME: ptr [[THIS:%.*]], float [[CM:%.*]], i1 [[TMP0:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CMP:%.*]] = call i1 @llvm.experimental.constrained.fcmps.f32(float [[CM]], float 0.000000e+00, metadata !"ole", metadata !"fpexcept.strict")
+; CHECK-NEXT:    [[CALL295:%.*]] = call float @test.specialized.1(ptr null, float 0.000000e+00, i1 false)
+; CHECK-NEXT:    ret float 0.000000e+00
+;
+entry:
+  %cmp = call i1 @llvm.experimental.constrained.fcmps.f32(float %cm, float 0.000000e+00, metadata !"ole", metadata !"fpexcept.strict") #0
+  %call295 = call float @test(ptr null, float 0.000000e+00, i1 false) #0
+  ret float 0.000000e+00
+}
+

@@ -415,6 +415,8 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {

for (unsigned Idx = 0, E = I.getNumOperands() - 1; Idx != E; ++Idx) {
Value *V = I.getOperand(Idx);
if (isa<MetadataAsValue>(V))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should perhaps ConstantFoldCall be handling this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe it is the findConstantFor -> getLatticeValueFor call that causes the problem, as the value is not in the lattice.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then should the isa(V) be moved inside findConstantFor?

if (isa<MetadataAsValue>(V))
  return nullptr;
if (auto *C = dyn_cast<Constant>(V))
  return C;
...

Would a metadata operand be possible to appear in anything else other than a call? If not, then let's leave it as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was wondering that - but calls should be the most common place. For other operations like load / stores the metadata will be an extra operands, not one of the ones we are already looking at like the Value or Pointer.
I believe this is where they are excluded for normal SCCP:

if (A.get()->getType()->isMetadataTy())

Fixes an issue reported from llvm#114964, where metadata arguments were looked at
to consider constants.
@davemgreen davemgreen force-pushed the gh-funcspec-badmetadata branch from ba4bd79 to f92ef76 Compare January 24, 2025 17:25
@davemgreen davemgreen merged commit 52bffdf into llvm:main Jan 25, 2025
8 checks passed
@davemgreen davemgreen deleted the gh-funcspec-badmetadata branch January 25, 2025 10:59
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.

3 participants