Skip to content

Commit abee4cd

Browse files
committed
reachable BB
1 parent d639ce4 commit abee4cd

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

llvm/lib/Analysis/IR2Vec.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
#include "llvm/Analysis/IR2Vec.h"
1515

16+
#include "llvm/ADT/PostOrderIterator.h"
1617
#include "llvm/ADT/Statistic.h"
18+
#include "llvm/IR/CFG.h"
19+
#include "llvm/IR/Function.h"
1720
#include "llvm/IR/Module.h"
1821
#include "llvm/IR/PassManager.h"
1922
#include "llvm/Support/Debug.h"
@@ -190,7 +193,8 @@ Embedding SymbolicEmbedder::getOperandEmbedding(const Value *Op) const {
190193
void SymbolicEmbedder::computeEmbeddings(const BasicBlock &BB) const {
191194
Embedding BBVector(Dimension, 0);
192195

193-
for (const auto &I : BB) {
196+
// We consider only the non-debug and non-pseudo instructions
197+
for (const auto &I : BB.instructionsWithoutDebug()) {
194198
Embedding InstVector(Dimension, 0);
195199

196200
const auto OpcVec = lookupVocab(I.getOpcodeName());
@@ -215,9 +219,12 @@ void SymbolicEmbedder::computeEmbeddings(const BasicBlock &BB) const {
215219
void SymbolicEmbedder::computeEmbeddings() const {
216220
if (F.isDeclaration())
217221
return;
218-
for (const auto &BB : F) {
219-
computeEmbeddings(BB);
220-
FuncVector += BBVecMap[&BB];
222+
223+
// Consider only the basic blocks that are reachable from entry
224+
ReversePostOrderTraversal<const Function *> RPOT(&F);
225+
for (const BasicBlock *BB : RPOT) {
226+
computeEmbeddings(*BB);
227+
FuncVector += BBVecMap[BB];
221228
}
222229
}
223230

llvm/test/Analysis/IR2Vec/dbg-inst.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt -passes='print<ir2vec>' -o /dev/null -ir2vec-vocab-path=%S/Inputs/dummy_3D_vocab.json %s 2>&1 | FileCheck %s
2+
3+
define void @bar2(ptr %foo) {
4+
store i32 0, ptr %foo, align 4
5+
tail call void @llvm.dbg.value(metadata !{}, i64 0, metadata !{}, metadata !{})
6+
ret void
7+
}
8+
9+
declare void @llvm.dbg.value(metadata, i64, metadata, metadata) nounwind readnone
10+
11+
; CHECK: Instruction vectors:
12+
; CHECK-NEXT: Instruction: store i32 0, ptr %foo, align 4 [ 7.00 8.00 9.00 ]
13+
; CHECK-NEXT: Instruction: ret void [ 0.00 0.00 0.00 ]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: opt -passes='print<ir2vec>' -o /dev/null -ir2vec-vocab-path=%S/Inputs/dummy_3D_vocab.json %s 2>&1 | FileCheck %s
2+
3+
define dso_local i32 @abc(i32 noundef %a, i32 noundef %b) #0 {
4+
entry:
5+
%retval = alloca i32, align 4
6+
%a.addr = alloca i32, align 4
7+
%b.addr = alloca i32, align 4
8+
store i32 %a, ptr %a.addr, align 4
9+
store i32 %b, ptr %b.addr, align 4
10+
%0 = load i32, ptr %a.addr, align 4
11+
%1 = load i32, ptr %b.addr, align 4
12+
%cmp = icmp sgt i32 %0, %1
13+
br i1 %cmp, label %if.then, label %if.else
14+
15+
if.then: ; preds = %entry
16+
%2 = load i32, ptr %b.addr, align 4
17+
store i32 %2, ptr %retval, align 4
18+
br label %return
19+
20+
if.else: ; preds = %entry
21+
%3 = load i32, ptr %a.addr, align 4
22+
store i32 %3, ptr %retval, align 4
23+
br label %return
24+
25+
unreachable: ; Unreachable
26+
store i32 0, ptr %retval, align 4
27+
br label %return
28+
29+
return: ; preds = %if.else, %if.then
30+
%4 = load i32, ptr %retval, align 4
31+
ret i32 %4
32+
}
33+
34+
; CHECK: Basic block vectors:
35+
; CHECK-NEXT: Basic block: entry:
36+
; CHECK-NEXT: [ 25.00 32.00 39.00 ]
37+
; CHECK-NEXT: Basic block: if.then:
38+
; CHECK-NEXT: [ 11.00 13.00 15.00 ]
39+
; CHECK-NEXT: Basic block: if.else:
40+
; CHECK-NEXT: [ 11.00 13.00 15.00 ]
41+
; CHECK-NEXT: Basic block: return:
42+
; CHECK-NEXT: [ 4.00 5.00 6.00 ]

0 commit comments

Comments
 (0)