diff options
author | Kristina Brooks <kristina@nym.hush.com> | 2018-10-08 17:29:39 +0000 |
---|---|---|
committer | Kristina Brooks <kristina@nym.hush.com> | 2018-10-08 17:29:39 +0000 |
commit | 125a52731ce604caabc379325f5cb498de0b9072 (patch) | |
tree | bf69a9808cd10f2978a7837de4ec2c5879fae7dc | |
parent | b8ee2e108cdcaea135bd04444bf2be02b8294ed2 (diff) |
Fix incorrect Twine usage in CFGPrinter
CFGPrinter (-view-cfg, -dot-cfg) invokes an undefined behaviour (dangling
pointer to rvalue) on IR files with branch weights. This patch fixes the
problem caused by Twine initialization and string conversion split into
two statements.
This change fixes the bug 37019. A similar patch to this problem was
provided in the llvmlite project
Patch by mcopik (Marcin Copik).
Differential Revision: https://reviews.llvm.org/D52933
-rw-r--r-- | llvm/include/llvm/Analysis/CFGPrinter.h | 3 | ||||
-rw-r--r-- | llvm/test/Other/cfg-printer-branch-weights.ll | 19 |
2 files changed, 20 insertions, 2 deletions
diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h index 5786769cc50..a4b642b9ea3 100644 --- a/llvm/include/llvm/Analysis/CFGPrinter.h +++ b/llvm/include/llvm/Analysis/CFGPrinter.h @@ -172,8 +172,7 @@ struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits { // Prepend a 'W' to indicate that this is a weight rather than the actual // profile count (due to scaling). - Twine Attrs = "label=\"W:" + Twine(Weight->getZExtValue()) + "\""; - return Attrs.str(); + return ("label=\"W:" + Twine(Weight->getZExtValue()) + "\"").str(); } }; } // End llvm namespace diff --git a/llvm/test/Other/cfg-printer-branch-weights.ll b/llvm/test/Other/cfg-printer-branch-weights.ll new file mode 100644 index 00000000000..b539e1cc677 --- /dev/null +++ b/llvm/test/Other/cfg-printer-branch-weights.ll @@ -0,0 +1,19 @@ +;RUN: opt < %s -analyze -dot-cfg 2>/dev/null +;RUN: FileCheck %s -input-file=cfg.f.dot + +define void @f(i32) { +entry: + %check = icmp sgt i32 %0, 0 + br i1 %check, label %if, label %exit, !prof !0 + +; CHECK: label="W:1" +; CHECK-NOT: ["]; +if: ; preds = %entry + br label %exit +; CHECK: label="W:200" +; CHECK-NOT: ["]; +exit: ; preds = %entry, %if + ret void +} + +!0 = !{!"branch_weights", i32 1, i32 200} |