Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
binaryen/src/ir/flat.h at main · WebAssembly/binaryen · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
WebAssembly
binaryen
Repository navigation
Code
Issues
334
(334)
Pull requests
252
(252)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
binaryen
/
src
/
ir
/
flat.h
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
134 lines (120 loc) · 4.3 KB
main
Breadcrumbs
binaryen
/
src
/
ir
/
flat.h
Copy path
Top
File metadata and controls
Code
Blame
134 lines (120 loc) · 4.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2019 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
//
Flattens code, removing nesting.e.g. an if return value would be
//
converted to a local
//
//
(i32.add
//
(if (..condition..)
//
(..if true..)
//
(..if false..)
//
)
//
(i32.const 1)
//
)
//
=>
//
(if (..condition..)
//
(local.set $temp
//
(..if true..)
//
)
//
(local.set $temp
//
(..if false..)
//
)
//
)
//
(i32.add
//
(local.get $temp)
//
(i32.const 1)
//
)
//
//
Formally, this pass flattens in the precise sense of
//
making the AST have these properties:
//
//
1. Aside from a local.set, the operands of an instruction must be a
//
local.get, a const, an unreachable, or a ref.as_non_null. Anything else
//
is written to a local earlier.
//
2. Disallow control flow (block, loop, if, and try) return values, and do
//
not allow the function body to have a concrete type, i.e., do not use
//
control flow to pass around values.
//
3. Disallow local.tee, setting a local is always done in a local.set
//
on a non-nested-expression location.
//
4. local.set cannot have an operand that is control flow (control flow with
//
values is prohibited already, but e.g. a block ending in unreachable,
//
which can normally be nested, is also disallowed).
//
//
Note: ref.as_non_null must be allowed in a nested position because we cannot
//
spill it to a local - the result is non-null, which is not allowable in a
//
local.
//
#
ifndef
wasm_ir_flat_h
#
define
wasm_ir_flat_h
#
include
"
ir/iteration.h
"
#
include
"
ir/properties.h
"
#
include
"
pass.h
"
#
include
"
wasm-traversal.h
"
namespace
wasm
::Flat {
inline
void
verifyFlatness
(Function* func) {
struct
VerifyFlatness
: public PostWalker<VerifyFlatness,
UnifiedExpressionVisitor<VerifyFlatness>> {
void
visitExpression
(Expression* curr) {
if
(
Properties::isControlFlowStructure
(curr)) {
verify
(!curr->
type
.
isConcrete
(),
"
control flow structures must not flow values
"
);
}
else
if
(
auto
* set = curr->
dynCast
<LocalSet>()) {
verify
(!set->
isTee
() || set->
type
== Type::unreachable,
"
tees are not allowed, only sets
"
);
verify
(!
Properties::isControlFlowStructure
(set->
value
),
"
set values cannot be control flow
"
);
}
else
{
for
(
auto
* child :
ChildIterator
(curr)) {
bool
isRefAsNonNull =
child->
is
<RefAs>() && child->
cast
<RefAs>()->
op
== RefAsNonNull;
verify
(
Properties::isConstantExpression
(child) ||
child->
is
<LocalGet>() || child->
is
<Unreachable>() ||
isRefAsNonNull,
"
instructions must only have constant expressions, local.get,
"
"
or unreachable as children
"
);
}
}
}
void
verify
(
bool
condition,
const
char
* message) {
if
(!condition) {
Fatal
() <<
"
IR must be flat: run --flatten beforehand (
"
<< message
<<
"
, in
"
<<
getFunction
()->
name
<<
'
)
'
;
}
}
};
VerifyFlatness verifier;
verifier.
walkFunction
(func);
verifier.
setFunction
(func);
verifier.
verify
(!func->
body
->
type
.
isConcrete
(),
"
function bodies must not flow values
"
);
}
inline
void
verifyFlatness
(Module*
module
) {
struct
VerifyFlatness
: public WalkerPass<
PostWalker<VerifyFlatness, UnifiedExpressionVisitor<VerifyFlatness>>> {
bool
isFunctionParallel
()
override
{
return
true
; }
bool
modifiesBinaryenIR
()
override
{
return
false
; }
std::unique_ptr<Pass>
create
()
override
{
return
std::make_unique<VerifyFlatness>();
}
void
visitFunction
(Function* func) {
verifyFlatness
(func); }
};
PassRunner
runner
(
module
);
VerifyFlatness
().
run
(&runner,
module
);
}
}
//
namespace wasm::Flat
#
endif
//
wasm_ir_flat_h
You can’t perform that action at this time.