Skip to content

Commit 0a89b16

Browse files
HerrCai0907atc-github
authored andcommitted
feat(cost_model): add performance cost model (#125)
using only size as the cost model is not accurate. This PR adds a cost model based on CPU performance. And assigns weights to the two through shrinkLevel and optimizeLevel.
1 parent 2e5ec2e commit 0a89b16

File tree

7 files changed

+506
-221
lines changed

7 files changed

+506
-221
lines changed

docs/infra/cost_model.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
Cost Model is average instructions count for each wasm op code.
1+
There are 2 kinds of cost model in WARPO.
2+
3+
**Performance Cost Model** is CPU execution time for each wasm op code.
4+
5+
**Size Cost Model** is average instructions count for each wasm op code.
26

37
Input is plain text file with following format.
48
`<wasm op code [string]> <average count of instructions [float]>`.
@@ -7,7 +11,7 @@ Input is plain text file with following format.
711
A special op code `func` is added to identify additional costs for each function.
812
:::
913

10-
preset cost models for 5 backend:
14+
preset size cost models for 5 backend:
1115

1216
- model/instruction_cost_aarch64_active_vb_warp.txt
1317
- model/instruction_cost_aarch64_vb_warp.txt

frontend/LinkedAPIOpt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <cassert>
2-
#include <iostream>
32

43
#include "CompilerImpl.hpp"
54
#include "LinkedAPI.hpp"

passes/AdvancedInlining.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// copy and modified from third_party/binaryen/src/passes/Inlining.cpp
1818

1919
#include <atomic>
20+
#include <cassert>
2021
#include <cstdint>
2122
#include <memory>
2223

@@ -37,6 +38,7 @@
3738
#include "pass.h"
3839
#include "passes/pass-utils.h"
3940
#include "support/name.h"
41+
#include "warpo/common/OptLevel.hpp"
4042
#include "warpo/support/Debug.hpp"
4143
#include "warpo/support/Opt.hpp"
4244
#include "wasm-builder.h"
@@ -101,7 +103,14 @@ struct FunctionInfo {
101103
budget += functionCost;
102104
}
103105
// calculate delta for each call
104-
float delta = inlinedCost - getOpcodeCost(Opcode::CALL);
106+
float const sizeCostDelta = inlinedCost - getOpcodeSizeCost(Opcode::CALL);
107+
float const performanceCostDelta = 0.0f - getOpcodePerformanceCost(Opcode::CALL) - getFunctionPerformanceCost();
108+
109+
float const optimizationLevel = static_cast<float>(common::getOptimizationLevel()) + 0.0001;
110+
float const shrinkLevel = static_cast<float>(common::getShrinkLevel()) + 0.0001;
111+
float const optRatio = optimizationLevel / (optimizationLevel + shrinkLevel);
112+
float const shrinkRatio = shrinkLevel / (optimizationLevel + shrinkLevel);
113+
float const delta = performanceCostDelta * optRatio + sizeCostDelta * shrinkRatio;
105114

106115
budget -= refs * delta;
107116

@@ -110,8 +119,8 @@ struct FunctionInfo {
110119

111120
bool const shouldInline = budget >= 0.0f;
112121
if (support::isDebug(PASS_NAME, funcName.str)) {
113-
fmt::println("[" PASS_NAME "] {} '{}', func_cost={}, refs={}, budget={}", shouldInline ? "inline" : "not inline",
114-
funcName.str, functionCost, refs.load(), budget);
122+
fmt::println("[" PASS_NAME "] {} '{}', func_cost={}, delta={}, refs={}, budget={}",
123+
shouldInline ? "inline" : "not inline", funcName.str, functionCost, delta, refs.load(), budget);
115124
}
116125
return shouldInline;
117126
}
@@ -157,8 +166,8 @@ struct FunctionInfoScanner : public WalkerPass<PostWalker<FunctionInfoScanner>>
157166
info.inliningMode = InliningMode::Uninlineble;
158167
}
159168

160-
float const bodyCost = measureCost(curr->body);
161-
info.functionCost = bodyCost + getFunctionCost();
169+
float const bodyCost = measureSizeCost(curr->body);
170+
info.functionCost = bodyCost + getFunctionSizeCost();
162171
info.inlinedCost = bodyCost;
163172
}
164173

0 commit comments

Comments
 (0)