[llvm] Fix: Distinguish CFI Metadata Checks in MergeFunctions Pass (PR #65963)

Oskar Wirga via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 21 17:25:14 PDT 2023


================
@@ -375,9 +375,35 @@ bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {
 }
 #endif
 
+// This function iterates over two functions and compares their
+// metadata to make sure we aren't merging functions which have
+// distinct metadata that is different. We don't care about
+// debug data here, it shouldn't change MIR like CFI would.
+static bool hasDistinctMetadataIntrinsic(const Function &F) {
+  for (const BasicBlock &BB : F) {
+    for (const Instruction &I : BB.instructionsWithoutDebug()) {
+      if (!isa<IntrinsicInst>(&I))
+        continue; // Only intrinsics can reference metadata.
+
+      // Iterate through operands, and return true if you find a distinct
+      // metadata operand.
+      for (unsigned i = 0, e = I.getNumOperands(); i != e;
+           ++i) { // Replaced InstLIt with I
+        MetadataAsValue *MDL = dyn_cast<MetadataAsValue>(
+            I.getOperand(i)); // Replaced InstLIt with I
----------------
oskarwirga wrote:

It doesn't look like we currently merge functions w/ constrained FP instrinsics:
```
; RUN: opt -passes=mergefunc -S < %s | FileCheck %s
; RUN: opt -passes=mergefunc -S < %s | FileCheck -check-prefix=MERGE %s

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"

declare float @llvm.experimental.constrained.fadd.f32(float, float, metadata, metadata)

!round.dynamic = !{}
!fpexcept.strict = !{}

define float @func1(float %a, float %b) {
; CHECK-LABEL: define float @func1(float %a, float %b)
  %result = call float @llvm.experimental.constrained.fadd.f32(float %a, float %b, metadata !"round.dynamic", metadata !"fpexcept.strict")
  ret float %result
}

define float @func2(float %a, float %b) {
; MERGE-NOT: define float @func2(float %a, float %b)
  %result = call float @llvm.experimental.constrained.fadd.f32(float %a, float %b, metadata !"round.dynamic", metadata !"fpexcept.strict")
  ret float %result
}
```

This fails to merge on master. 

https://github.com/llvm/llvm-project/pull/65963


More information about the llvm-commits mailing list