Skip to content

Commit 58370e0

Browse files
authored
Prune unused constant expressions recursively. (#678)
1 parent 1de83c1 commit 58370e0

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/irgen.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,16 +633,8 @@ function add_kernel_state!(mod::LLVM.Module)
633633

634634
# ensure the old (stateless) functions don't have uses anymore, and remove them
635635
for f in keys(workmap)
636-
for use in uses(f)
637-
val = user(use)
638-
if val isa ConstantExpr
639-
# XXX: shouldn't clone_into! remove unused CEs?
640-
isempty(uses(val)) || error("old function still has uses (via a constant expr)")
641-
LLVM.unsafe_destroy!(val)
642-
else
643-
error("old function still has uses")
644-
end
645-
end
636+
prune_constexpr_uses!(f)
637+
@assert isempty(uses(f))
646638
replace_metadata_uses!(f, workmap[f])
647639
erase!(f)
648640
end

src/metal.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,7 @@ function add_global_address_spaces!(@nospecialize(job::CompilerJob), mod::LLVM.M
406406

407407
# delete old globals
408408
for (old, new) in global_map
409-
for use in uses(old)
410-
val = user(use)
411-
if val isa ConstantExpr
412-
# XXX: shouldn't clone_into! remove unused CEs?
413-
isempty(uses(val)) || error("old function still has uses (via a constant expr)")
414-
LLVM.unsafe_destroy!(val)
415-
end
416-
end
409+
prune_constexpr_uses!(old)
417410
@assert isempty(uses(old))
418411
replace_metadata_uses!(old, new)
419412
erase!(old)
@@ -637,12 +630,7 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
637630
# drop unused constants that may be referring to the old functions
638631
# XXX: can we do this differently?
639632
for f in worklist
640-
for use in uses(f)
641-
val = user(use)
642-
if val isa LLVM.ConstantExpr && isempty(uses(val))
643-
LLVM.unsafe_destroy!(val)
644-
end
645-
end
633+
prune_constexpr_uses!(f)
646634
end
647635

648636
# update other uses of the old function, modifying call sites to pass the arguments

src/utils.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,20 @@ macro unlocked(ex)
132132
end
133133
esc(combinedef(def))
134134
end
135+
136+
137+
## constant expression pruning
138+
139+
# for some reason, after cloning the LLVM IR can contain unused constant expressions.
140+
# these result in false positives when checking that values are unused and can be deleted.
141+
# this helper function removes such unused constant expression uses of a value.
142+
# the process needs to be recursive, as constant expressions can refer to one another.
143+
function prune_constexpr_uses!(root::LLVM.Value)
144+
for use in uses(root)
145+
val = user(use)
146+
if val isa ConstantExpr
147+
prune_constexpr_uses!(val)
148+
isempty(uses(val)) && LLVM.unsafe_destroy!(val)
149+
end
150+
end
151+
end

0 commit comments

Comments
 (0)