Skip to content

Commit 63cabba

Browse files
Fix compile tests to avoid device initialization (#396)
Leftover cleanup from #377. Avoids creating runtime handles in compile-only command and session tests so AMDGPU-enabled builds do not initialize the HIP HAL just to format or build compiler invocations. The tests now pass the intended backend directly: explicit CPU and AMDGPU cases stay explicit, while cases that previously used the default handle backend use `kDefaultBackend` without initializing a runtime device. Signed-off-by: Sambhav Jain <sambhav@alumni.stanford.edu> Co-authored-by: GPT 5.5 <codex@openai.com>
1 parent 2be3fe2 commit 63cabba

2 files changed

Lines changed: 30 additions & 82 deletions

File tree

tests/test_compile_command.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ using namespace fusilli;
2020
static std::string kGraphName = "test_compile_command";
2121

2222
TEST_CASE("CompileCommand::build with CPU backend", "[CompileCommand]") {
23-
// Create test handle for CPU backend.
24-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::CPU));
25-
2623
// Create temporary cache files.
2724
FUSILLI_REQUIRE_ASSIGN(
2825
CacheFile input,
@@ -43,7 +40,7 @@ TEST_CASE("CompileCommand::build with CPU backend", "[CompileCommand]") {
4340

4441
// Build the compile command.
4542
CompileCommand cmd =
46-
CompileCommand::build(handle.getBackend(), input, output, statistics);
43+
CompileCommand::build(Backend::CPU, input, output, statistics);
4744

4845
// Verify the command arguments.
4946
const auto &args = cmd.getArgs();
@@ -101,9 +98,6 @@ TEST_CASE("CompileCommand::build with CPU backend", "[CompileCommand]") {
10198

10299
#if defined(FUSILLI_ENABLE_AMDGPU)
103100
TEST_CASE("CompileCommand::build with AMDGPU backend", "[CompileCommand]") {
104-
// Create test handle for AMDGPU backend.
105-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::AMDGPU));
106-
107101
// Create temporary cache files.
108102
FUSILLI_REQUIRE_ASSIGN(
109103
CacheFile input,
@@ -124,7 +118,7 @@ TEST_CASE("CompileCommand::build with AMDGPU backend", "[CompileCommand]") {
124118

125119
// Build the compile command.
126120
CompileCommand cmd =
127-
CompileCommand::build(handle.getBackend(), input, output, statistics);
121+
CompileCommand::build(Backend::AMDGPU, input, output, statistics);
128122

129123
// Verify the command arguments.
130124
const auto &args = cmd.getArgs();
@@ -160,9 +154,6 @@ TEST_CASE("CompileCommand::build with AMDGPU backend", "[CompileCommand]") {
160154
#endif
161155

162156
TEST_CASE("CompileCommand::toString format", "[CompileCommand]") {
163-
// Create test handle.
164-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::CPU));
165-
166157
// Create temporary cache files.
167158
FUSILLI_REQUIRE_ASSIGN(
168159
CacheFile input,
@@ -180,7 +171,7 @@ TEST_CASE("CompileCommand::toString format", "[CompileCommand]") {
180171

181172
// Build the compile command.
182173
CompileCommand cmd =
183-
CompileCommand::build(handle.getBackend(), input, output, statistics);
174+
CompileCommand::build(Backend::CPU, input, output, statistics);
184175

185176
// Get string representation.
186177
std::string cmdStr = cmd.toString();
@@ -200,9 +191,6 @@ TEST_CASE("CompileCommand::toString format", "[CompileCommand]") {
200191
}
201192

202193
TEST_CASE("CompileCommand::writeTo", "[CompileCommand]") {
203-
// Create test handle.
204-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
205-
206194
// Create temporary cache files.
207195
FUSILLI_REQUIRE_ASSIGN(
208196
CacheFile input,
@@ -223,7 +211,7 @@ TEST_CASE("CompileCommand::writeTo", "[CompileCommand]") {
223211

224212
// Build the compile command.
225213
CompileCommand cmd =
226-
CompileCommand::build(handle.getBackend(), input, output, statistics);
214+
CompileCommand::build(kDefaultBackend, input, output, statistics);
227215

228216
// Write to cache file.
229217
FUSILLI_REQUIRE_OK(cmd.writeTo(commandFile));
@@ -236,9 +224,6 @@ TEST_CASE("CompileCommand::writeTo", "[CompileCommand]") {
236224
}
237225

238226
TEST_CASE("CompileCommand::getArgs", "[CompileCommand]") {
239-
// Create test handle.
240-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
241-
242227
// Create temporary cache files.
243228
FUSILLI_REQUIRE_ASSIGN(
244229
CacheFile input,
@@ -256,7 +241,7 @@ TEST_CASE("CompileCommand::getArgs", "[CompileCommand]") {
256241

257242
// Build the compile command.
258243
CompileCommand cmd =
259-
CompileCommand::build(handle.getBackend(), input, output, statistics);
244+
CompileCommand::build(kDefaultBackend, input, output, statistics);
260245

261246
// Get args and verify structure.
262247
const auto &args = cmd.getArgs();
@@ -276,9 +261,6 @@ TEST_CASE("CompileCommand::getArgs", "[CompileCommand]") {
276261
}
277262

278263
TEST_CASE("CompileCommand round-trip serialization", "[CompileCommand]") {
279-
// Create test handle.
280-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
281-
282264
// Create temporary cache files.
283265
FUSILLI_REQUIRE_ASSIGN(
284266
CacheFile input,
@@ -299,12 +281,12 @@ TEST_CASE("CompileCommand round-trip serialization", "[CompileCommand]") {
299281

300282
// Build and write command.
301283
CompileCommand cmd1 =
302-
CompileCommand::build(handle.getBackend(), input, output, statistics);
284+
CompileCommand::build(kDefaultBackend, input, output, statistics);
303285
FUSILLI_REQUIRE_OK(cmd1.writeTo(commandFile));
304286

305287
// Build another command with the same parameters.
306288
CompileCommand cmd2 =
307-
CompileCommand::build(handle.getBackend(), input, output, statistics);
289+
CompileCommand::build(kDefaultBackend, input, output, statistics);
308290

309291
// Read the serialized command.
310292
FUSILLI_REQUIRE_ASSIGN(std::string serializedCmd, commandFile.read());

tests/test_compile_session.cpp

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,8 @@ TEST_CASE("CompileContext::createSession with CPU backend",
9191
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
9292
REQUIRE(context != nullptr);
9393

94-
// Create a handle for CPU backend.
95-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::CPU));
96-
9794
// Create a session.
98-
auto maybeSession = context->createSession(handle.getBackend());
95+
auto maybeSession = context->createSession(Backend::CPU);
9996
FUSILLI_REQUIRE_OK(maybeSession);
10097

10198
FUSILLI_REQUIRE_ASSIGN(CompileSession session, std::move(maybeSession));
@@ -111,11 +108,8 @@ TEST_CASE("CompileContext::createSession with AMDGPU backend",
111108
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
112109
REQUIRE(context != nullptr);
113110

114-
// Create a handle for AMDGPU backend.
115-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::AMDGPU));
116-
117111
// Create a session.
118-
auto maybeSession = context->createSession(handle.getBackend());
112+
auto maybeSession = context->createSession(Backend::AMDGPU);
119113
FUSILLI_REQUIRE_OK(maybeSession);
120114

121115
FUSILLI_REQUIRE_ASSIGN(CompileSession session, std::move(maybeSession));
@@ -131,16 +125,13 @@ TEST_CASE("CompileContext supports multiple sessions",
131125
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
132126
REQUIRE(context != nullptr);
133127

134-
// Create a handle.
135-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
136-
137128
// Create multiple sessions from the same context.
138129
FUSILLI_REQUIRE_ASSIGN(auto session1,
139-
context->createSession(handle.getBackend()));
130+
context->createSession(kDefaultBackend));
140131
FUSILLI_REQUIRE_ASSIGN(auto session2,
141-
context->createSession(handle.getBackend()));
132+
context->createSession(kDefaultBackend));
142133
FUSILLI_REQUIRE_ASSIGN(auto session3,
143-
context->createSession(handle.getBackend()));
134+
context->createSession(kDefaultBackend));
144135

145136
// All sessions should be valid.
146137
SUCCEED("Multiple sessions created successfully");
@@ -150,9 +141,7 @@ TEST_CASE("CompileSession::addFlag", "[CompileSession]") {
150141
// Get the shared compiler context and create a session.
151142
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
152143
REQUIRE(context != nullptr);
153-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
154-
FUSILLI_REQUIRE_ASSIGN(auto session,
155-
context->createSession(handle.getBackend()));
144+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
156145

157146
// Add a flag.
158147
FUSILLI_REQUIRE_OK(session.addFlag("--iree-opt-level=O3"));
@@ -165,9 +154,7 @@ TEST_CASE("CompileSession::addFlags", "[CompileSession]") {
165154
// Get the shared compiler context and create a session.
166155
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
167156
REQUIRE(context != nullptr);
168-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
169-
FUSILLI_REQUIRE_ASSIGN(auto session,
170-
context->createSession(handle.getBackend()));
157+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
171158

172159
// Add multiple flags.
173160
std::vector<std::string> flags = {
@@ -185,9 +172,7 @@ TEST_CASE("CompileSession::compile with valid MLIR",
185172
// Get the shared compiler context and create a session.
186173
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
187174
REQUIRE(context != nullptr);
188-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
189-
FUSILLI_REQUIRE_ASSIGN(auto session,
190-
context->createSession(handle.getBackend()));
175+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
191176

192177
// Create temporary cache files.
193178
FUSILLI_REQUIRE_ASSIGN(
@@ -220,9 +205,7 @@ TEST_CASE("CompileSession::compile with custom flags",
220205
// Get the shared compiler context and create a session.
221206
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
222207
REQUIRE(context != nullptr);
223-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
224-
FUSILLI_REQUIRE_ASSIGN(auto session,
225-
context->createSession(handle.getBackend()));
208+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
226209

227210
// Add custom optimization flags.
228211
FUSILLI_REQUIRE_OK(session.addFlag("--iree-opt-level=O3"));
@@ -260,9 +243,7 @@ TEST_CASE("CompileSession::compile with invalid MLIR",
260243
// Get the shared compiler context and create a session.
261244
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
262245
REQUIRE(context != nullptr);
263-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
264-
FUSILLI_REQUIRE_ASSIGN(auto session,
265-
context->createSession(handle.getBackend()));
246+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
266247

267248
// Create temporary cache files.
268249
FUSILLI_REQUIRE_ASSIGN(
@@ -292,9 +273,7 @@ TEST_CASE("CompileSession::compile with missing input file",
292273
// Get the shared compiler context and create a session.
293274
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
294275
REQUIRE(context != nullptr);
295-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
296-
FUSILLI_REQUIRE_ASSIGN(auto session,
297-
context->createSession(handle.getBackend()));
276+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
298277

299278
// Create cache files but don't write to input.
300279
FUSILLI_REQUIRE_ASSIGN(
@@ -319,9 +298,8 @@ TEST_CASE("CompileSession move semantics", "[CompileSession]") {
319298
// Get the shared compiler context and create sessions.
320299
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
321300
REQUIRE(context != nullptr);
322-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
323301
FUSILLI_REQUIRE_ASSIGN(auto session1,
324-
context->createSession(handle.getBackend()));
302+
context->createSession(kDefaultBackend));
325303

326304
// Add a flag to session1.
327305
FUSILLI_REQUIRE_OK(session1.addFlag("--iree-opt-level=O3"));
@@ -341,9 +319,7 @@ TEST_CASE("CompileSession::compile with AMDGPU backend",
341319
// Get the shared compiler context and create a session for AMDGPU.
342320
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
343321
REQUIRE(context != nullptr);
344-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::AMDGPU));
345-
FUSILLI_REQUIRE_ASSIGN(auto session,
346-
context->createSession(handle.getBackend()));
322+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(Backend::AMDGPU));
347323

348324
// Create temporary cache files.
349325
FUSILLI_REQUIRE_ASSIGN(
@@ -376,12 +352,11 @@ TEST_CASE("CompileSession cleanup on destruction", "[CompileSession]") {
376352
// Get the shared compiler context.
377353
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
378354
REQUIRE(context != nullptr);
379-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
380355

381356
// Create a session in a nested scope.
382357
{
383358
FUSILLI_REQUIRE_ASSIGN(auto session,
384-
context->createSession(handle.getBackend()));
359+
context->createSession(kDefaultBackend));
385360

386361
// Use the session.
387362
FUSILLI_REQUIRE_OK(session.addFlag("--iree-opt-level=O3"));
@@ -392,7 +367,7 @@ TEST_CASE("CompileSession cleanup on destruction", "[CompileSession]") {
392367
// Should be able to create a new session after the previous one was
393368
// destroyed.
394369
FUSILLI_REQUIRE_ASSIGN(auto session2,
395-
context->createSession(handle.getBackend()));
370+
context->createSession(kDefaultBackend));
396371
FUSILLI_REQUIRE_OK(session2.addFlag("--iree-opt-level=O2"));
397372

398373
SUCCEED("Session cleanup and recreation works correctly");
@@ -402,9 +377,7 @@ TEST_CASE("CompileSession with invalid flag", "[CompileSession][error]") {
402377
// Get the shared compiler context and create a session.
403378
FUSILLI_REQUIRE_ASSIGN(auto *context, CompileContext::create());
404379
REQUIRE(context != nullptr);
405-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
406-
FUSILLI_REQUIRE_ASSIGN(auto session,
407-
context->createSession(handle.getBackend()));
380+
FUSILLI_REQUIRE_ASSIGN(auto session, context->createSession(kDefaultBackend));
408381

409382
// Try to add an invalid flag.
410383
auto result = session.addFlag("--this-is-not-a-valid-iree-flag-xyz123");
@@ -442,7 +415,6 @@ TEST_CASE("Multiple CompileContexts can coexist", "[CompileContext]") {
442415
// ===========================================================================
443416

444417
TEST_CASE("CompileSession::build with CPU backend", "[CompileSession]") {
445-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(Backend::CPU));
446418
FUSILLI_REQUIRE_ASSIGN(CacheFile input,
447419
CacheFile::create(kGraphName, "input.mlir", true));
448420
FUSILLI_REQUIRE_ASSIGN(CacheFile output,
@@ -457,7 +429,7 @@ TEST_CASE("CompileSession::build with CPU backend", "[CompileSession]") {
457429

458430
FUSILLI_REQUIRE_ASSIGN(
459431
CompileSession session,
460-
CompileSession::build(handle.getBackend(), input, output, statistics));
432+
CompileSession::build(Backend::CPU, input, output, statistics));
461433

462434
// Verify that flags were added (including statistics flags).
463435
const auto &args = session.getArgs();
@@ -481,7 +453,6 @@ TEST_CASE("CompileSession::build with CPU backend", "[CompileSession]") {
481453
}
482454

483455
TEST_CASE("CompileSession::toString format", "[CompileSession]") {
484-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
485456
FUSILLI_REQUIRE_ASSIGN(CacheFile input,
486457
CacheFile::create(kGraphName, "input.mlir", true));
487458
FUSILLI_REQUIRE_ASSIGN(CacheFile output,
@@ -496,7 +467,7 @@ TEST_CASE("CompileSession::toString format", "[CompileSession]") {
496467

497468
FUSILLI_REQUIRE_ASSIGN(
498469
CompileSession session,
499-
CompileSession::build(handle.getBackend(), input, output, statistics));
470+
CompileSession::build(kDefaultBackend, input, output, statistics));
500471

501472
std::string cmdStr = session.toString();
502473

@@ -507,7 +478,6 @@ TEST_CASE("CompileSession::toString format", "[CompileSession]") {
507478
}
508479

509480
TEST_CASE("CompileSession::writeTo", "[CompileSession]") {
510-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
511481
FUSILLI_REQUIRE_ASSIGN(CacheFile input,
512482
CacheFile::create(kGraphName, "input.mlir", true));
513483
FUSILLI_REQUIRE_ASSIGN(CacheFile output,
@@ -524,7 +494,7 @@ TEST_CASE("CompileSession::writeTo", "[CompileSession]") {
524494

525495
FUSILLI_REQUIRE_ASSIGN(
526496
CompileSession session,
527-
CompileSession::build(handle.getBackend(), input, output, statistics));
497+
CompileSession::build(kDefaultBackend, input, output, statistics));
528498

529499
FUSILLI_REQUIRE_OK(session.writeTo(commandFile));
530500

@@ -534,7 +504,6 @@ TEST_CASE("CompileSession::writeTo", "[CompileSession]") {
534504
}
535505

536506
TEST_CASE("CompileSession::execute with valid MLIR", "[CompileSession]") {
537-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
538507
FUSILLI_REQUIRE_ASSIGN(CacheFile input,
539508
CacheFile::create(kGraphName, "input.mlir", true));
540509
FUSILLI_REQUIRE_ASSIGN(CacheFile output,
@@ -552,7 +521,7 @@ TEST_CASE("CompileSession::execute with valid MLIR", "[CompileSession]") {
552521

553522
FUSILLI_REQUIRE_ASSIGN(
554523
CompileSession session,
555-
CompileSession::build(handle.getBackend(), input, output, statistics));
524+
CompileSession::build(kDefaultBackend, input, output, statistics));
556525

557526
// Execute compilation.
558527
FUSILLI_REQUIRE_OK(session.execute());
@@ -567,7 +536,6 @@ TEST_CASE("CompileSession::execute with valid MLIR", "[CompileSession]") {
567536
}
568537

569538
TEST_CASE("CompileSession::getArgs", "[CompileSession]") {
570-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
571539
FUSILLI_REQUIRE_ASSIGN(CacheFile input,
572540
CacheFile::create(kGraphName, "input.mlir", true));
573541
FUSILLI_REQUIRE_ASSIGN(CacheFile output,
@@ -582,7 +550,7 @@ TEST_CASE("CompileSession::getArgs", "[CompileSession]") {
582550

583551
FUSILLI_REQUIRE_ASSIGN(
584552
CompileSession session,
585-
CompileSession::build(handle.getBackend(), input, output, statistics));
553+
CompileSession::build(kDefaultBackend, input, output, statistics));
586554

587555
const auto &args = session.getArgs();
588556

@@ -600,9 +568,8 @@ TEST_CASE("CompileSession::addFlag with tuning spec path",
600568
// Verify tuning spec path flag is accepted by C API.
601569
FUSILLI_REQUIRE_ASSIGN(CompileContext * context, CompileContext::create());
602570
REQUIRE(context != nullptr);
603-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
604571
FUSILLI_REQUIRE_ASSIGN(CompileSession session,
605-
context->createSession(handle.getBackend()));
572+
context->createSession(kDefaultBackend));
606573

607574
ErrorObject result =
608575
session.addFlag("--iree-codegen-tuning-spec-path=" +
@@ -622,9 +589,8 @@ TEST_CASE("CompileSession::compile with tuning spec",
622589
// End-to-end compilation with tuning spec via C API.
623590
FUSILLI_REQUIRE_ASSIGN(CompileContext * context, CompileContext::create());
624591
REQUIRE(context != nullptr);
625-
FUSILLI_REQUIRE_ASSIGN(Handle handle, Handle::create(kDefaultBackend));
626592
FUSILLI_REQUIRE_ASSIGN(CompileSession session,
627-
context->createSession(handle.getBackend()));
593+
context->createSession(kDefaultBackend));
628594

629595
FUSILLI_REQUIRE_OK(session.addFlag("--iree-codegen-tuning-spec-path=" +
630596
getTestTuningSpecPath().generic_string()));

0 commit comments

Comments
 (0)