Commit 236c69b
committed
Allow removing unused pointerref
This allows the julia-level optimizer to drop unused pointerref.
This matches LLVM in that unused non-volatile loads are allowed to
be dropped, despite them having possibly observable side effects
(segfaults), since those are modeled as undefined. When heavily
optimizing code we frequently saw a lot of left over pointerref
calls resulting from inlining `length(::SimpleVector)`. We have a
special tfunc for that call (that can give us the result as a const),
but inline it anyway and thus end up with a pointerref whose result
is unused that we didn't used to be able to eliminate. E.g.
```
julia> f(T::Type{S}) where {S} = Val(length(T.parameters))
f (generic function with 1 method)
```
Before:
```
julia> @code_typed f(Int)
CodeInfo(
1 ─ %1 = (Base.getfield)(T, :parameters)::SimpleVector
│ %2 = $(Expr(:gc_preserve_begin, :(%1)))
│ %3 = $(Expr(:foreigncall, :(:jl_value_ptr), Ptr{Nothing}, svec(Any), :(:ccall), 1, :(%1)))::Ptr{Nothing}
│ %4 = (Base.bitcast)(Ptr{Int64}, %3)::Ptr{Int64}
│ (Base.pointerref)(%4, 1, 1)::Int64
│ $(Expr(:gc_preserve_end, :(%2)))
└── return $(QuoteNode(Val{0}()))
) => Val{0}
```
After:
```
julia> @code_typed f(Int)
CodeInfo(
1 ─ %1 = (Base.getfield)(T, :parameters)::SimpleVector
│ %2 = $(Expr(:gc_preserve_begin, :(%1)))
│ $(Expr(:foreigncall, :(:jl_value_ptr), Ptr{Nothing}, svec(Any), :(:ccall), 1, :(%1)))::Ptr{Nothing}
│ $(Expr(:gc_preserve_end, :(%2)))
└── return $(QuoteNode(Val{0}()))
) => Val{0}
```
Of course we still have the useless foreigncall. That is outside
the scope of this PR, but I'm hoping to have a general solution
in the future to mark foreigncalls as effect free if unused.1 parent 19a0f71 commit 236c69b
File tree
4 files changed
+27
-1
lines changed- base/compiler
- ssair
- test/compiler
4 files changed
+27
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
263 | 266 | | |
264 | 267 | | |
265 | 268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1315 | 1315 | | |
1316 | 1316 | | |
1317 | 1317 | | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
1318 | 1325 | | |
1319 | 1326 | | |
1320 | 1327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
0 commit comments