From f7fe7ea24d368f45deee1aed5e4c582ac69edd0b Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Mon, 9 Nov 2020 09:19:11 +0000 Subject: [PATCH] [MergeFunctions] fix function attribute comparison in FunctionComparator The comparison of AttributeSets stopped after seeing a matching type attribute. Subsequent mismatching attributes were not detected causing a crash. --- llvm/lib/Transforms/Utils/FunctionComparator.cpp | 11 ++++++++--- .../Transforms/MergeFunc/mismatching-attr-crash.ll | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 llvm/test/Transforms/MergeFunc/mismatching-attr-crash.ll diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp index dfab936..f25c4e5 100644 --- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp +++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp @@ -124,12 +124,17 @@ int FunctionComparator::cmpAttrs(const AttributeList L, Type *TyL = LA.getValueAsType(); Type *TyR = RA.getValueAsType(); - if (TyL && TyR) - return cmpTypes(TyL, TyR); + if (TyL && TyR) { + if (int Res = cmpTypes(TyL, TyR)) + return Res; + continue; + } // Two pointers, at least one null, so the comparison result is // independent of the value of a real pointer. - return cmpNumbers((uint64_t)TyL, (uint64_t)TyR); + if (int Res = cmpNumbers((uint64_t)TyL, (uint64_t)TyR)) + return Res; + continue; } if (LA < RA) return -1; diff --git a/llvm/test/Transforms/MergeFunc/mismatching-attr-crash.ll b/llvm/test/Transforms/MergeFunc/mismatching-attr-crash.ll new file mode 100644 index 0000000..e3f81c5 --- /dev/null +++ b/llvm/test/Transforms/MergeFunc/mismatching-attr-crash.ll @@ -0,0 +1,21 @@ +; RUN: opt -S -mergefunc %s | FileCheck %s + +; CHECK-LABEL: define void @foo +; CHECK: call void %bc +define void @foo(i8* byval %a0, i8* swiftself %a4) { +entry: + %bc = bitcast i8* %a0 to void (i8*, i8*)* + call void %bc(i8* byval %a0, i8* swiftself %a4) + ret void +} + +; CHECK-LABEL: define void @bar +; CHECK: call void %bc +define void @bar(i8* byval(i8) %a0, i8** swifterror %a4) { +entry: + %bc = bitcast i8* %a0 to void (i8*, i8**)* + call void %bc(i8* byval(i8) %a0, i8** swifterror %a4) + ret void +} + + -- 2.7.4