Fix name legalization.

This commit is contained in:
Doru Bercea 2019-12-19 14:46:18 -05:00
parent 50ac6e7bce
commit e1f6ae1336
1 changed files with 24 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <regex>
#include <tuple>
#include <numeric>
#include <string>
#include "mlir/Analysis/Verifier.h"
#include "mlir/Dialect/StandardOps/Ops.h"
@ -30,6 +31,29 @@
namespace onnf {
namespace {
void replaceAll(
std::string& str, const std::string& from, const std::string& to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing
// 'x' with 'yx'
}
}
std::string legalize_name(std::string name) {
std::replace(name.begin(), name.end(), '/', '_');
std::replace(name.begin(), name.end(), '-', '_');
replaceAll(name, ":", "_colon_");
// If tensor name starts with a number, prepend n to make it a legal c++
// identifier.
if (name.size() > 0 && isdigit(name.at(0)))
name.insert(0, 1, 'n');
return name;
}
struct OnnxOnnfSymbolMapping {
/*!
* Get MLIR tensor by onnx tensor name.