1313#include < catch2/matchers/catch_matchers_string.hpp>
1414
1515#include < algorithm>
16+ #include < cassert>
1617#include < filesystem>
18+ #include < fstream>
1719#include < string>
1820#include < vector>
1921
2022using namespace fusilli ;
2123
2224static std::string kGraphName = " test_compile_session" ;
2325
26+ // Returns the path to a shared tuning spec file for tests. The file is created
27+ // once and persists for the process lifetime because IREE caches parsed tuning
28+ // specs on the dialect instance keyed by path, and the tuning spec flag itself
29+ // is a process-wide static. Deleting the file would break subsequent
30+ // compilations that hit the cached path.
31+ static std::filesystem::path getTestTuningSpecPath () {
32+ static std::filesystem::path path = [] {
33+ std::filesystem::path dir =
34+ std::filesystem::temp_directory_path () / " fusilli_test_tuning_specs" ;
35+ std::filesystem::create_directories (dir);
36+ std::filesystem::path p = dir / " tuning_spec.mlir" ;
37+ std::ofstream ofs (p);
38+ assert (ofs.is_open () && " Failed to create test tuning spec file" );
39+ ofs << R"(
40+ module attributes {transform.with_named_sequence} {
41+ transform.named_sequence @__kernel_config(%arg0: !transform.any_op {transform.consumed})
42+ -> !transform.any_op attributes {iree_codegen.tuning_spec_entrypoint} {
43+ transform.yield %arg0 : !transform.any_op
44+ }
45+ }
46+ )" ;
47+ ofs.close ();
48+ return p;
49+ }();
50+ return path;
51+ }
52+
2453TEST_CASE (" CompileContext::create successfully loads library" ,
2554 " [CompileContext]" ) {
2655 // Get the shared compiler context.
@@ -530,10 +559,9 @@ TEST_CASE("CompileSession::addFlag with tuning spec path",
530559 FUSILLI_REQUIRE_ASSIGN (CompileSession session,
531560 context->createSession (handle));
532561
533- std::string tuningSpecPath = " /tmp/test_tuning_spec.mlir" ;
534-
535562 ErrorObject result =
536- session.addFlag (" --iree-codegen-tuning-spec-path=" + tuningSpecPath);
563+ session.addFlag (" --iree-codegen-tuning-spec-path=" +
564+ getTestTuningSpecPath ().generic_string ());
537565 FUSILLI_REQUIRE_OK (result);
538566
539567 const std::vector<std::string> &args = session.getArgs ();
@@ -553,35 +581,32 @@ TEST_CASE("CompileSession::compile with tuning spec",
553581 FUSILLI_REQUIRE_ASSIGN (CompileSession session,
554582 context->createSession (handle));
555583
556- // Create a minimal no-op tuning spec.
557- FUSILLI_REQUIRE_ASSIGN (
558- CacheFile tuningSpec,
559- CacheFile::create (kGraphName , " test_tuning_spec.mlir" , /* remove=*/ true ));
560-
561- std::string tuningSpecContent = R"(
562- module attributes {transform.with_named_sequence} {
563- transform.named_sequence @__kernel_config(%arg0: !transform.any_op {transform.consumed})
564- -> !transform.any_op attributes {iree_codegen.tuning_spec_entrypoint} {
565- transform.yield %arg0 : !transform.any_op
566- }
567- }
568- )" ;
569- FUSILLI_REQUIRE_OK (tuningSpec.write (tuningSpecContent));
570-
571584 FUSILLI_REQUIRE_OK (session.addFlag (" --iree-codegen-tuning-spec-path=" +
572- tuningSpec.path .string ()));
585+ getTestTuningSpecPath ().generic_string ()));
586+
587+ // Create input/output files in cache (these will auto-cleanup with
588+ // remove=true).
589+ std::string graphName = " test_compile_session_with_tuning_spec" ;
573590 FUSILLI_REQUIRE_ASSIGN (
574591 CacheFile input,
575- CacheFile::create (kGraphName , " input_with_spec .mlir" , /* remove=*/ true ));
592+ CacheFile::create (graphName , " input .mlir" , /* remove=*/ true ));
576593 FUSILLI_REQUIRE_ASSIGN (
577594 CacheFile output,
578- CacheFile::create (kGraphName , " output_with_spec .vmfb" , /* remove=*/ true ));
595+ CacheFile::create (graphName , " output .vmfb" , /* remove=*/ true ));
579596
580- std::string mlirContent = getSimpleMLIRModule ();
597+ // Write a simple MLIR module to the input file.
598+ std::string mlirContent = R"(
599+ module {
600+ func.func @tuning_spec_test(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<3x3xf32> {
601+ %0 = arith.mulf %arg0, %arg1 : tensor<3x3xf32>
602+ return %0 : tensor<3x3xf32>
603+ }
604+ }
605+ )" ;
581606 FUSILLI_REQUIRE_OK (input.write (mlirContent));
582607
583- ErrorObject compileResult =
584- session. compile (input. path . string (), output.path .string ());
608+ ErrorObject compileResult = session. compile (input. path . generic_string (),
609+ output.path .generic_string ());
585610 FUSILLI_REQUIRE_OK (compileResult);
586611 REQUIRE (std::filesystem::exists (output.path ));
587612 REQUIRE (std::filesystem::file_size (output.path ) > 0 );
0 commit comments