onnx-mlir icon indicating copy to clipboard operation
onnx-mlir copied to clipboard

Missing endianess conversion for dense constant lowering to LLVM dialect

Open Ansaya opened this issue 8 months ago • 6 comments

I am using onnx-mlir to build models for Sparc CPU target, which is big-endian, and I am facing a data conversion issue caused by the string type initialization used for large dense constants. MLIR's IR and LLVM's IR expect the data representation to be little-endian until the code generation for the target system is performed; then, data is transformed to adhere to the target system's endianness according to the data type. During the Krnl to LLVM lowering, dense raw data bigger than 1Kb is lowered to LLVM global constant as a string of bytes, thus losing the information relative to the actual data type of the initialized elements. Consequently, when code generation happens, the data endianness is not updated to that of the target system since string type is endianness independent. This leads to an incorrect constant data initialization for big-endian targets.

I understand that converting the dense constant data to a string saves space since LLVM IR stores floating-point constants as double, even for smaller data types, however, this removes the data type information needed for the endianness fix during the code generation step. I want to contribute with a fix to this, but I would like to know which solution you think is the best for the case:

  1. Avoid the dense-to-string conversion and keep the correct data type (this is the solution I tried, and it solves the issue, but it results in the initialization data being promoted to double until the code generation phase)
  2. Keep the current implementation and perform the byte-swapping operations on the generated string of bytes (I do not know which is the proper way to implement this, but this may be a better solution from the space-saving point of view)

Please let me know if you are interested and what you think is the best approach to solve the issue.

Ansaya avatar Jun 17 '24 21:06 Ansaya