[PATCH] D154119: Fix: Distinguish CFI Metadata Checks in MergeFunctions Pass

Oskar Wirga via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 3 12:30:49 PDT 2023


oskarwirga updated this revision to Diff 536869.
oskarwirga added a comment.

Added checks for distinct metadata in operands to prevent merging unless it's just debug info. This ensures that distinct metadata, which should be treated as different, is not merged.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154119/new/

https://reviews.llvm.org/D154119

Files:
  llvm/lib/Transforms/Utils/FunctionComparator.cpp
  llvm/test/Transforms/MergeFunc/cfi-function-merging.ll


Index: llvm/test/Transforms/MergeFunc/cfi-function-merging.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/MergeFunc/cfi-function-merging.ll
@@ -0,0 +1,41 @@
+;; Check the cases involving internal CFI instrumented functions where we do not expect functions to be merged.
+; RUN: opt -S -passes=mergefunc < %s | FileCheck %s
+
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-none-linux-android28"
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+declare i1 @llvm.type.test(ptr, metadata) #6
+
+define internal void @A__on_zero_sharedEv(ptr noundef nonnull align 8 dereferenceable(32) %this) unnamed_addr #3 align 2 {
+; CHECK-LABEL: @A__on_zero_sharedEv
+entry:
+  %this.addr = alloca ptr, align 8
+  store ptr %this, ptr %this.addr, align 8
+  %this1 = load ptr, ptr %this.addr, align 8
+  %vtable = load ptr, ptr %this1, align 8
+  %0 = call i1 @llvm.type.test(ptr %vtable, metadata !11), !nosanitize !47
+  ret void
+}
+
+; Function Attrs: mustprogress noinline nounwind optnone uwtable
+define internal void @B__on_zero_sharedEv(ptr noundef nonnull align 8 dereferenceable(32) %this) unnamed_addr #3 align 2 {
+; CHECK-LABEL: @B__on_zero_sharedEv
+entry:
+  %this.addr = alloca ptr, align 8
+  store ptr %this, ptr %this.addr, align 8
+  %this1 = load ptr, ptr %this.addr, align 8
+  %vtable = load ptr, ptr %this1, align 8
+  %0 = call i1 @llvm.type.test(ptr %vtable, metadata !22), !nosanitize !47
+  ret void
+}
+
+attributes #3 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fix-cortex-a53-835769,+neon,+outline-atomics,+v8a" }
+attributes #6 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
+
+!10 = !{i64 16, !11}
+!11 = distinct !{}
+!21 = !{i64 16, !22}
+!22 = distinct !{}
+!47 = !{}
Index: llvm/lib/Transforms/Utils/FunctionComparator.cpp
===================================================================
--- llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/GlobalValue.h"
@@ -852,6 +853,29 @@
       for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
         Value *OpL = InstL->getOperand(i);
         Value *OpR = InstR->getOperand(i);
+
+        // Distinct metadata is different and shouldn't be merged unless it's
+        // just debug info
+        if (auto *MDL = dyn_cast<MetadataAsValue>(OpL)) {
+          auto *MDR = cast<MetadataAsValue>(OpR);
+
+          const MDNode *MDNodeL = dyn_cast<MDNode>(MDL->getMetadata());
+          const MDNode *MDNodeR = dyn_cast<MDNode>(MDR->getMetadata());
+          if (MDNodeL && MDNodeR) {
+            if (MDNodeL->isDistinct() && MDNodeR->isDistinct()) {
+              if (isa<llvm::DINode>(MDNodeL) && isa<llvm::DINode>(MDNodeR))
+                return 0;
+              return -1;
+            }
+            if (MDNodeL->isDistinct()) {
+              return 1;
+            }
+            if (MDNodeR->isDistinct()) {
+              return -1;
+            }
+          }
+        }
+
         if (int Res = cmpValues(OpL, OpR))
           return Res;
         // cmpValues should ensure this is true.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154119.536869.patch
Type: text/x-patch
Size: 3629 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230703/6417f8c9/attachment.bin>


More information about the llvm-commits mailing list