Skip to content

Commit

Permalink
Fix incorrect Twine usage in CFGPrinter
Browse files Browse the repository at this point in the history
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

llvm-svn: 343984
  • Loading branch information
Kristina Brooks committed Oct 8, 2018
1 parent 9b5a495 commit 4f197cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions llvm/include/llvm/Analysis/CFGPrinter.h
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions 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}

0 comments on commit 4f197cd

Please sign in to comment.