Skip to content

Commit f359c70

Browse files
committed
[PTX/GCN] Replace tabs into spaces such that @code_for and @code_diff match
1 parent 6b06bd1 commit f359c70

5 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/cleanup/cleanup.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ cleanup_code(type, code) = cleanup_code(type, code, true, (;))
2121
cleanup_code(type, code, dbinfo) = cleanup_code(type, code, dbinfo, (;))
2222
cleanup_code(_, c, _, _) = c # default is no cleanup
2323

24+
25+
function replace_tabs(tab_width)
26+
# Matches any line with at least one tab in it
27+
line_with_tabs = r"^.*?\t.*$"m
28+
29+
function replace_all_tabs_within_line(line)
30+
buf = IOBuffer(; sizehint=ncodeunits(line))
31+
column = 0
32+
for char in line
33+
if char == '\t'
34+
required_spaces = tab_width - (column % tab_width)
35+
foreach(_ -> print(buf, ' '), 1:required_spaces)
36+
column += required_spaces
37+
else
38+
print(buf, char)
39+
column += 1
40+
end
41+
end
42+
return String(take!(buf))
43+
end
44+
45+
return line_with_tabs => replace_all_tabs_within_line
46+
end
47+
48+
2449
include("julia_names.jl")
2550
include("typed_ir.jl")
2651
include("llvm_ir.jl")

src/cleanup/gcn.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ function cleanup_code(::Val{:gcn}, c, dbinfo, cleanup_opts)
6868
r"\s*; -- Begin function.+$"m => "",
6969
extra_patterns...,
7070
)
71+
72+
# GCN doesn't have any instruction guards, so 4 spaces is enough.
73+
c = replace(c, replace_tabs(4))
7174
return rstrip(c) # remove trailing newlines
7275
end
7376

@@ -81,7 +84,7 @@ function align_instruction_operand(column)
8184
m = match(operand_regex, c)
8285
isnothing(m) && return c
8386

84-
mnemonic_end = m.offsets[1]
87+
mnemonic_end = m.offsets[1]
8588

8689
if all(isspace, m[2])
8790
# In case the operands are just trailing spaces, return only the mnemonic

src/cleanup/ptx.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function cleanup_code(::Val{:ptx}, c, dbinfo, cleanup_opts)
4545
push!(extra_patterns, align_instruction_predicates(align_preds))
4646
end
4747

48-
return replace(c,
48+
c = replace(c,
4949
# Parameter names are always local to a function, so this should never create ambiguous code.
5050
trim_parameter_names(),
5151
# Generic match to cleanup external functions as well
@@ -66,6 +66,10 @@ function cleanup_code(::Val{:ptx}, c, dbinfo, cleanup_opts)
6666
r"\R{2,}" => "\n",
6767
extra_patterns...
6868
)
69+
70+
# Replace tabs by 8 spaces, as it gives enough space for instruction guards.
71+
# Some patterns may depend on tabs, so we must replace them last.
72+
return replace(c, replace_tabs(8))
6973
end
7074

7175

src/cleanup/sass.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ function cleanup_code(::Val{:sass}, c, dbinfo, cleanup_opts)
3131
push!(extra_patterns, r"; Location .+\R" => "") # Remove location comments
3232
end
3333

34-
return replace(c, extra_patterns...)
34+
c = replace(c, extra_patterns...)
35+
36+
# SASS output uses 8 spaces as indent, but there is still some tabs in some places.
37+
return replace(c, replace_tabs(8))
3538
end

test/cleanup.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ end
217217
# Others
218218
@test !has_trailing_spaces(cleaned_ptx)
219219
@test count(r"\R{2,}", cleaned_ptx) == 0 # no empty lines
220+
@test count('\t', cleaned_ptx) == 0 # no tabs
220221

221222
# Make sure we didn't remove any instruction by mistake
222223
@test count(';', ptx_sample) == count(';', cleaned_ptx)
@@ -239,6 +240,7 @@ end
239240
@test !has_trailing_spaces(cleaned_ptx)
240241
@test count(r"\R{2,}", cleaned_ptx) == 0 # no empty lines
241242
@test !endswith(cleaned_ptx, r"\R") # no trailing newlines
243+
@test count('\t', cleaned_ptx) == 0 # no tabs
242244

243245
# Make sure we didn't remove any instruction by mistake
244246
@test count(';', ptx_sample) == count(';', cleaned_ptx)
@@ -260,6 +262,7 @@ end
260262
@test !has_trailing_spaces(cleaned_ptx)
261263
@test count(r"\R{2,}", cleaned_ptx) == 0 # no empty lines
262264
@test !endswith(cleaned_ptx, r"\R") # no trailing newlines
265+
@test count('\t', cleaned_ptx) == 0 # no tabs
263266

264267
# Make sure we didn't remove any instruction by mistake
265268
@test count(';', ptx_sample) == count(';', cleaned_ptx)
@@ -284,6 +287,7 @@ end
284287
# Others
285288
@test !has_trailing_spaces(cleaned_sass)
286289
@test !endswith(cleaned_sass, r"\R") # no trailing newlines
290+
@test count('\t', cleaned_sass) == 0 # no tabs
287291

288292
# Location comments should be removed with `dbinfo=false`
289293
cleaned_sass_no_loc = CDC.cleanup_code(Val(:sass), sass_sample, false)
@@ -321,6 +325,7 @@ end
321325
@test !has_trailing_spaces(cleaned_gcn)
322326
@test count(r"\R{2,}", cleaned_gcn) == 0 # no empty lines
323327
@test !endswith(cleaned_gcn, r"\R") # no trailing newlines
328+
@test count('\t', cleaned_gcn) == 0 # no tabs
324329
end
325330
end
326331

@@ -345,6 +350,7 @@ end
345350
@test !has_trailing_spaces(cleaned_spirv)
346351
@test count(r"\R{2,}", cleaned_spirv) == 0 # no empty lines
347352
@test !endswith(cleaned_spirv, r"\R") # no trailing newlines
353+
@test count('\t', cleaned_spirv) == 0 # no tabs
348354
end
349355
end
350356

0 commit comments

Comments
 (0)