aboutsummaryrefslogtreecommitdiff
path: root/backend/tools/MIRTool/MIRToolStageTest.cmake
blob: 0d3ef2ef52d8bf53eddeb95e9a7f1680527c717f (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# SPDX-License-Identifier: GPL-3.0-or-later

if(NOT DEFINED TOOL OR NOT DEFINED INPUT OR NOT DEFINED WORK_DIRECTORY)
  message(FATAL_ERROR "MIR tool stage test is missing a required path")
endif()

file(REMOVE_RECURSE "${WORK_DIRECTORY}")
file(MAKE_DIRECTORY "${WORK_DIRECTORY}")

set(
  Stages
  prelegalizer-mir
  legalized-mir
  regbank-mir
  selected-mir
  post-ssa-mir
  post-ra-mir
  final-mir
)
set(
  StageOptions
  --verify
  -stop-after=legalizer
  -stop-after=regbankselect
  -stop-after=instruction-select
  -stop-after=twoaddressinstruction
  -stop-after=PBQPAllocator
  -stop-after=final-encodability-verifier
)
set(StageIndex 0)
foreach(Stage IN LISTS Stages)
  list(GET StageOptions ${StageIndex} StageOption)
  set(StageOutput "${WORK_DIRECTORY}/${Stage}.mir")
  set(RepeatedOutput "${WORK_DIRECTORY}/${Stage}.repeat.mir")
  set(CanonicalOutput "${WORK_DIRECTORY}/${Stage}.canonical.mir")
  execute_process(
    COMMAND "${TOOL}" "${INPUT}" "${StageOption}" -o "${StageOutput}"
    RESULT_VARIABLE StageResult
    ERROR_VARIABLE StageDiagnostics
  )
  if(NOT StageResult EQUAL 0)
    message(FATAL_ERROR "${Stage} production failed: ${StageDiagnostics}")
  endif()
  execute_process(
    COMMAND "${TOOL}" "${INPUT}" "${StageOption}" -o "${RepeatedOutput}"
    RESULT_VARIABLE RepeatedResult
    ERROR_VARIABLE RepeatedDiagnostics
  )
  if(NOT RepeatedResult EQUAL 0)
    message(FATAL_ERROR "repeated ${Stage} production failed: ${RepeatedDiagnostics}")
  endif()
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E compare_files "${StageOutput}" "${RepeatedOutput}"
    RESULT_VARIABLE DeterminismResult
  )
  if(NOT DeterminismResult EQUAL 0)
    message(FATAL_ERROR "${Stage} output is not deterministic")
  endif()
  execute_process(
    COMMAND "${TOOL}" --verify "${StageOutput}" -o "${CanonicalOutput}"
    RESULT_VARIABLE VerifyResult
    ERROR_VARIABLE VerifyDiagnostics
  )
  if(NOT VerifyResult EQUAL 0)
    message(FATAL_ERROR "${Stage} output failed strict MIR reload: ${VerifyDiagnostics}")
  endif()
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E compare_files "${StageOutput}" "${CanonicalOutput}"
    RESULT_VARIABLE CanonicalResult
  )
  if(NOT CanonicalResult EQUAL 0)
    message(FATAL_ERROR "${Stage} output is not canonical after strict reload")
  endif()
  math(EXPR StageIndex "${StageIndex} + 1")
endforeach()

set(LLVMContractOutput "${WORK_DIRECTORY}/llvm-contract.mir")
execute_process(
  COMMAND
    "${TOOL}" --verify "${INPUT}" -mtriple=riscv64
    -mattr=+m,+a,+f,+d,+c -relocation-model=pic -code-model=small -simplify-mir
    -verify-machineinstrs -o "${LLVMContractOutput}"
  RESULT_VARIABLE LLVMContractResult
  ERROR_VARIABLE LLVMContractDiagnostics
)
if(NOT LLVMContractResult EQUAL 0)
  message(FATAL_ERROR "LLVM llc-compatible invocation failed: ${LLVMContractDiagnostics}")
endif()
file(SIZE "${LLVMContractOutput}" LLVMContractOutputSize)
if(LLVMContractOutputSize EQUAL 0)
  message(FATAL_ERROR "LLVM llc-compatible invocation produced no MIR")
endif()

foreach(
  RemovedOption
  IN ITEMS
    --features=+m
    --output=-
)
  execute_process(
    COMMAND "${TOOL}" "${INPUT}" "${RemovedOption}"
    RESULT_VARIABLE RemovedOptionResult
    ERROR_VARIABLE RemovedOptionDiagnostics
  )
  if(RemovedOptionResult EQUAL 0 OR
     NOT RemovedOptionDiagnostics MATCHES "unknown command-line option")
    message(FATAL_ERROR "removed option ${RemovedOption} remains accepted")
  endif()
endforeach()

set(PassList "${WORK_DIRECTORY}/passes.txt")
execute_process(
  COMMAND "${TOOL}" --list-passes "${INPUT}" -o "${PassList}"
  RESULT_VARIABLE ListResult
  ERROR_VARIABLE ListDiagnostics
)
if(NOT ListResult EQUAL 0)
  message(FATAL_ERROR "pass listing failed: ${ListDiagnostics}")
endif()
file(READ "${PassList}" PassListText)
foreach(RequiredPass IN ITEMS legalizer regbankselect instruction-select prologepilog)
  if(NOT PassListText MATCHES "(^|\n)${RequiredPass}(\n|$)")
    message(FATAL_ERROR "canonical pass list is missing ${RequiredPass}")
  endif()
endforeach()

set(LegalizedOutput "${WORK_DIRECTORY}/run-pass.mir")
execute_process(
  COMMAND
    "${TOOL}" "${INPUT}" -run-pass legalizer -o
    "${LegalizedOutput}"
  RESULT_VARIABLE RunPassResult
  ERROR_VARIABLE RunPassDiagnostics
)
if(NOT RunPassResult EQUAL 0)
  message(FATAL_ERROR "single-pass execution failed: ${RunPassDiagnostics}")
endif()

set(RegBankOutput "${WORK_DIRECTORY}/passes.mir")
execute_process(
  COMMAND
    "${TOOL}" "${INPUT}" -passes legalizer,regbankselect -o
    "${RegBankOutput}"
  RESULT_VARIABLE PassesResult
  ERROR_VARIABLE PassesDiagnostics
)
if(NOT PassesResult EQUAL 0)
  message(FATAL_ERROR "explicit pass pipeline failed: ${PassesDiagnostics}")
endif()

set(StoppedOutput "${WORK_DIRECTORY}/stopped.mir")
execute_process(
  COMMAND
    "${TOOL}" "${INPUT}" -stop-after legalizer -o
    "${StoppedOutput}"
  RESULT_VARIABLE StopResult
  ERROR_VARIABLE StopDiagnostics
)
if(NOT StopResult EQUAL 0)
  message(FATAL_ERROR "stop-after control failed: ${StopDiagnostics}")
endif()
execute_process(
  COMMAND "${TOOL}" --verify "${StoppedOutput}"
  RESULT_VARIABLE StopVerifyResult
  ERROR_VARIABLE StopVerifyDiagnostics
)
if(NOT StopVerifyResult EQUAL 0)
  message(FATAL_ERROR "stop-after output failed strict reload: ${StopVerifyDiagnostics}")
endif()

execute_process(
  COMMAND
    "${TOOL}" "${INPUT}" -filetype=null -print-before legalizer -print-after legalizer
  RESULT_VARIABLE PrintResult
  ERROR_VARIABLE PrintDiagnostics
)
if(NOT PrintResult EQUAL 0)
  message(FATAL_ERROR "pass snapshot controls failed: ${PrintDiagnostics}")
endif()
if(NOT PrintDiagnostics MATCHES "[*][*][*] prelegalizer-mir legalizer#1" OR
   NOT PrintDiagnostics MATCHES "[*][*][*] legalized-mir legalizer#1")
  message(FATAL_ERROR "before/after pass snapshots are incomplete")
endif()

execute_process(
  COMMAND
    "${TOOL}" "${INPUT}" -filetype=null --stage-snapshots
  RESULT_VARIABLE SnapshotResult
  ERROR_VARIABLE SnapshotDiagnostics
)
if(NOT SnapshotResult EQUAL 0)
  message(FATAL_ERROR "stage snapshot execution failed: ${SnapshotDiagnostics}")
endif()
foreach(Stage IN LISTS Stages)
  if(NOT SnapshotDiagnostics MATCHES "[*][*][*] ${Stage}")
    message(FATAL_ERROR "stage snapshots are missing ${Stage}")
  endif()
endforeach()

file(REMOVE_RECURSE "${WORK_DIRECTORY}")