<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/137622>137622</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `--mlir-print-ir-tree-dir` fails with "Error while creating directory ...: No such file or directory" if symbol name contains path separator
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          i-ky
      </td>
    </tr>
</table>

<pre>
    Consider the following example MLIR code in `/tmp/test.mlir`:
```mlir
module @"/foo/bar/" {}
```

Running `mlir-opt --canonicalize --mlir-print-ir-before-all --mlir-print-ir-after-all /tmp/test.mlir` outputs the following (as expected):
```
// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
module @"/foo/bar/" {
}


// -----// IR Dump After Canonicalizer (canonicalize) //----- //
module @"/foo/bar/" {
}


module @"/foo/bar/" {
}

```

But when `--mlir-print-ir-tree-dir=/tmp/test` is added to the command - `mlir-opt --canonicalize --mlir-print-ir-before-all --mlir-print-ir-after-all --mlir-print-ir-tree-dir=/tmp/test /tmp/test.mlir` - the output is the following (note the errors):
```
Error while creating directory /tmp/test/builtin_module_/foo/bar/: No such file or directory
Error while creating directory /tmp/test/builtin_module_/foo/bar/: No such file or directory
module @"/foo/bar/" {
}

```
...and the `/tmp/test/` directory is empty.

There is no issue if `/` is swapped for `\` (I am on MacOS, on Windows it may be the opposite):
```
mlir-opt --canonicalize --mlir-print-ir-before-all --mlir-print-ir-after-all --mlir-print-ir-tree-dir=/tmp/test /tmp/test.mlir
module @"\\foo\\bar\\" {
}

tree /tmp/test                                                                                                                                              
/tmp/test
└── builtin_module_\foo\bar\
    ├── 0_canonicalize.mlir
    └── 1_canonicalize.mlir

2 directories, 2 files
```

The code responsible for generating paths is sanitizing op names by replacing `.` with `_`, but does nothing to symbol names:
https://github.com/llvm/llvm-project/blob/e086d7b1464ad091ae849cb907f8647c8439cb66/mlir/lib/Pass/IRPrinting.cpp#L207-L240

[Symbol names can be string literals](https://mlir.llvm.org/docs/LangRef/#identifiers-and-keywords), so there is a need to sanitize them before using in filesystem paths.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVk1v2zgQ_TX0ZSBBpr4PPjh2DQRId4u0wB4DShxZ3FKkQFJ13V-_IKW2juN2W-yiqCAkE1IzfDPvcSbMWnFUiBuS35F8v2KT67XZiOj9edVoft7stLKCowHXI3RaSn0S6gj4kQ2jRHj9cP8IreYIQgEpEkIPbhj9T7QuHqQwfjHdkmTrjfCG1WQ7aD5JBJIlhFJCD53WhB4aZgg9EEqBlHek3F86ejvZPk5KeQhLpEiPDqKoZUor0TIpPiFEUdgZjVAuEiZqsNMGIybliy3WOTRh5yZ00JMbJ2ev0ie0Yhbw44itQ05ofZWit30WB4j8s9j3j7CfhhHuAhzYXUA2PuRlDoTWMLuFCIv9g2VLtkvl5vfbSLY--18G5Gc9nvN-Nzk49Rhkds2iM4gRF4ak-2c0egaFBcY5cnA6sNjqYWCKQ_S_K-hHUX1DalGANwvOo34hOaUdhlU0Rht7U3av_BaceiERWoPMeV8uDLZOm_Pzk33xJyGdUE8zNU_XpKRb-EODndoeOh9Rm6-xfvVh_0E9cRx7xn3prnuUdy6SC9DCAg6jO8dzmHc9GvSLSoOwdkIQ3RJkEZc9sXFEDp02YSPf-Q1Cq3tgA2gFr1n751tCd97-SyiuTxaEg4GdoZn51OOorXB4k9HfRqLXFOQ7ku88C8HwRATjBhf-lOcR4bd65iZ5IYpkS15RUmf-nY0q-WLAtY4_V2EpQbINMZcAuxsBkqdLJj9X99Lr1rHrm14k2dIv8hVovdBouED2ZQ99F_ofRzBoRz_ZG4lBuEdUaOb7OzLX2yBspoQTn_yaHkGxAS00ZzA4StYuEzj2Wj8J1_s_nsK92EEzOeAa_Z1xvf_QabDnodFyjjJrvHduDGYYKEfh-qmJWz0QepDyw-df0Wj039iG9iF1Q-g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