Struggling with simple mlir-translate to spir-v

While I’m not sure exactly what above issue is, I guess it’s related null-support in glsl.

Since reversing(glsl → spirv → mlir) seemed difficult, I read through spirv dialect documentation to write below mlir. This compiled successfully to glsl with mlir-translate & spirv-cross.

simpleFrag.mlir

spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
  spirv.GlobalVariable @in {location = 0 : i32} : !spirv.ptr<vector<3xf32>, Input>
  spirv.GlobalVariable @out {location = 0 : i32} : !spirv.ptr<vector<4xf32>, Output>

  spirv.func @vec3ToVec4(%arg1 : vector<3xf32>) -> vector<4xf32> "None" {
    %int_1 = spirv.Constant 1.0: f32
    %1 = spirv.CompositeExtract %arg1[0 : i32] :vector<3xf32>
    %2 = spirv.CompositeExtract %arg1[1 : i32] :vector<3xf32>
    %3 = spirv.CompositeExtract %arg1[2: i32] :vector<3xf32>
    %4 = spirv.CompositeConstruct %1, %2, %3, %int_1 : (f32,f32,f32,f32) -> vector<4xf32>
    spirv.ReturnValue %4 : vector<4xf32>
  }

  spirv.func @main() -> () "None" {
    %in_ptr = spirv.mlir.addressof @in : !spirv.ptr<vector<3xf32>, Input>
    %out_ptr = spirv.mlir.addressof @out : !spirv.ptr<vector<4xf32>, Output>
    %in = spirv.Load "Input" %in_ptr : vector<3xf32>

    %vec4 = spirv.FunctionCall @vec3ToVec4(%in) : (vector<3xf32>) ->  (vector<4xf32>)
    spirv.Store "Output" %out_ptr, %vec4 : vector<4xf32>

    spirv.Return
  }
  spirv.EntryPoint "Fragment" @main
}

compiling:

➜  mlir-playground git:(main) ✗ mlir-translate -no-implicit-module -serialize-spirv simpleFrag.mlir | spirv-cross -
#version 450

layout(location = 0) in vec3 _in;
layout(location = 0) out vec4 _out;

vec4 vec3ToVec4(vec3 _10)
{
    return vec4(_10, 1.0);
}

void main()
{
    _out = vec3ToVec4(_in);
}

I think I’ve got a very rough understanding of mlir, so thank you for your support and good documentation.

With that said, I will leave some opinions on the doc for future new-comers.

  1. mlir-opt and mlir-translate should be the first thing to introduce to new-comers. While I’m unfamiliar with any compiler IRs, coding a .mlir and mlir-translate gave me a very good idea of what mlir is capable of. (Related: [doc] mlir-translate / mlir-opt - #30 by clattner)
  2. The relationship between various dialects are obscure. I wish a comprehensive directed graph diagram of dialects like this one was available at the root of dialect documentation.

Anyways, thank you!