Skip to content

Commit 411d428

Browse files
committed
More cleanup fixes for PTX and GCN, more tests
1 parent 8ec0d06 commit 411d428

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/cleanup/gcn.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# The GCN output of LLVM's AMDGPU backend has a comment with the mangled function name
3-
const GCN_FUNC_NAME_COMMENT_REGEX = r"; -- Begin function "m * MANGLED_NAME_REGEX * r"$"m
3+
const GCN_FUNC_NAME_COMMENT_REGEX = r".globl\s+"m * MANGLED_NAME_REGEX * r"\b"m
44

55

66
function cleanup_code(::Val{:gcn}, c, dbinfo, cleanup_opts)
@@ -48,12 +48,13 @@ function cleanup_code(::Val{:gcn}, c, dbinfo, cleanup_opts)
4848
push!(extra_patterns, align_instruction_operand(align_operands))
4949
end
5050

51-
return replace(c,
51+
c = replace(c,
5252
llvm_module_name_patterns()...,
5353
# This "begin" comment is redundant
5454
r"\s*; -- Begin function.+$"m => "",
5555
extra_patterns...,
5656
)
57+
return rstrip(c) # remove trailing newlines
5758
end
5859

5960

src/cleanup/ptx.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function cleanup_code(::Val{:ptx}, c, dbinfo, cleanup_opts)
2525
if !get(cleanup_opts, :keep_block_comments, false)
2626
# Block comments are placed after labels, or at the beginning of a line
2727
# They look like "some_label: // %blabla" or "// %blabla".
28-
push!(extra_patterns, r"^([a-zA-Z0-9_$%]+:)?(?:\s*)\/\/ %.+"m => s"\1")
28+
push!(extra_patterns,
29+
r"^([a-zA-Z0-9_$%]+:)(?:\s*)\/\/ %.+"m => s"\1",
30+
r"^\/\/ %.+\R"m => "", # when the whole line is a comment like such, remove the newline as well
31+
)
2932
end
3033
if (align_preds = get(cleanup_opts, :align_preds, 8); align_preds > 0)
3134
# Align guard predicates such that the beginning of the instruction is at the
@@ -170,8 +173,17 @@ function indent_ptx_function_calls()
170173
indent = " "^8
171174
call_attrs = strip(m[1]) # remove any '\r' on Windows
172175
func_name = strip(m[2]) # and again
173-
params = replace(m[3], "\n" => "\n" * indent)
174-
return "call" * m[1] * m[2] * ", (" * params * "\n" * indent * ");"
176+
if m[3] == "\n"
177+
# No parameters
178+
params = ""
179+
else
180+
params = replace(m[3], "\n" => "\n" * indent) * "\n" * indent
181+
end
182+
# For some reason, the `replace` in `cleanup_code(::Val{:ptx})` doesn't replace
183+
# the function name here, so we have to do it manually.
184+
func_name = replace(func_name, llvm_module_name_patterns()...)
185+
# Rebuild the function call in a cleaner way
186+
return "call" * call_attrs * " " * func_name * ", (" * params * ");"
175187
end
176188

177189
return ptx_call_regex => indent_call_params

test/cleanup.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import CodeDiffs.Cleanup as CDC
55
is_removed(needle, present_there, not_there) =
66
occursin(needle, present_there) && !occursin(needle, not_there)
77

8-
has_trailling_spaces(str) = occursin(r"\h+(\n|$)"m, str)
8+
has_trailing_spaces(str) = occursin(r"\h+(\n|$)"m, str)
99

1010

1111
@testset "Demangling" begin
@@ -200,10 +200,10 @@ end
200200
@test is_removed(r"\t@%", ptx_sample, cleaned_ptx)
201201

202202
# Reformat function calls
203-
@test is_removed(r"call.+?,\R", ptx_sample, cleaned_ptx)
203+
@test is_removed(r"call\S+\s+\R", ptx_sample, cleaned_ptx)
204204

205205
# Others
206-
@test !has_trailling_spaces(cleaned_ptx)
206+
@test !has_trailing_spaces(cleaned_ptx)
207207
@test count(r"\R{2,}", cleaned_ptx) == 0 # no empty lines
208208

209209
# Make sure we didn't remove any instruction by mistake
@@ -220,15 +220,17 @@ end
220220
println(TEST_IO, cleaned_ptx)
221221

222222
# Proper cleanup of each parameter name
223-
@test count(func_name * r"\d+_param_\d+\b", ptx_sample) == 3
224-
@test count(func_name * r"_param_\d+\b", cleaned_ptx) == 3
223+
@test count(func_name * r"_\d+_param_\d+\b", ptx_sample) == 6
224+
@test count(r"\bparam_\d+\b", cleaned_ptx) == 6
225+
226+
# Others
227+
@test !has_trailing_spaces(cleaned_ptx)
228+
@test count(r"\R{2,}", cleaned_ptx) == 0 # no empty lines
229+
@test !endswith(cleaned_ptx, r"\R") # no trailing newlines
225230

226231
# Make sure we didn't remove any instruction by mistake
227232
@test count(';', ptx_sample) == count(';', cleaned_ptx)
228233
end
229-
230-
231-
# TODO: calls with 0 parameters are not reformatted correctly
232234
end
233235

234236

@@ -257,9 +259,14 @@ end
257259
# Code-gen comments are all removed
258260
@test is_removed(r"; %L\d+", gcn_sample, cleaned_gcn)
259261
@test is_removed(r"; %bb\.\d+:", gcn_sample, cleaned_gcn)
260-
@test is_removed("divergent unreachable", gcn_sample, cleaned_gcn)
262+
@test is_removed("; divergent unreachable", gcn_sample, cleaned_gcn)
261263
@test is_removed("; -- Begin function", gcn_sample, cleaned_gcn)
262264
@test is_removed("; -- End function", gcn_sample, cleaned_gcn)
265+
266+
# Others
267+
@test !has_trailing_spaces(cleaned_gcn)
268+
@test count(r"\R{2,}", cleaned_gcn) == 0 # no empty lines
269+
@test !endswith(cleaned_gcn, r"\R") # no trailing newlines
263270
end
264271
end
265272

0 commit comments

Comments
 (0)