Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Add bitcast operation #284

Merged
merged 4 commits into from
Jan 4, 2025
Merged

[IR] Add bitcast operation #284

merged 4 commits into from
Jan 4, 2025

Conversation

chhzh123
Copy link
Member

@chhzh123 chhzh123 commented Jan 4, 2025

Description

This PR exposes the bitcast operation to the frontend (which has already been added in MLIR previously). The API is just .bitcast(). By default, if .bitcast() is operated on an integer, it will be cast to a floating point; while .bitcast() is operated on a floating point, it will be cast to an integer with the same bitwidth. Notice this is different from a normal cast, where a normal cast preserves the actual value of the variable, while bitcast preserves the bits.

Examples

def test_bitcast_uint2float():
    def kernel(A: uint32[10, 10]) -> float32[10, 10]:
        B: float32[10, 10]
        for i, j in allo.grid(10, 10):
            B[i, j] = A[i, j].bitcast()
        return B

    s = allo.customize(kernel)
    print(s.module)
    mod = s.build()

    A_np = np.random.randint(100, size=(10, 10)).astype(np.uint32)
    B_np = mod(A_np)
    answer = np.frombuffer(A_np.tobytes(), np.float32).reshape((10, 10))
    assert np.array_equal(B_np, answer)

    code = str(s.build(target="vhls"))
    assert "union" in code and "uint32" in code
    print("Passed!")


def test_bitcast_float2uint():
    def kernel(A: float32[10, 10]) -> uint32[10, 10]:
        B: uint32[10, 10]
        for i, j in allo.grid(10, 10):
            B[i, j] = A[i, j].bitcast()
        return B

    s = allo.customize(kernel)
    print(s.module)
    mod = s.build()

    A_np = np.random.rand(10, 10).astype(np.float32)
    B_np = mod(A_np)
    answer = np.frombuffer(A_np.tobytes(), np.uint32).reshape((10, 10))
    assert np.array_equal(B_np, answer)

    code = str(s.build(target="vhls"))
    assert "union" in code and "uint32" in code
    print("Passed!")
module {
  func.func @kernel(%arg0: memref<10x10xf32>) -> memref<10x10xi32> attributes {itypes = "_", otypes = "u"} {
    %alloc = memref.alloc() {name = "B", unsigned} : memref<10x10xi32>
    affine.for %arg1 = 0 to 10 {
      affine.for %arg2 = 0 to 10 {
        %0 = affine.load %arg0[%arg1, %arg2] {from = "A"} : memref<10x10xf32>
        %1 = arith.bitcast %0 {unsigned} : f32 to i32
        affine.store %1, %alloc[%arg1, %arg2] {to = "B"} : memref<10x10xi32>
      } {loop_name = "j"}
    } {loop_name = "i", op_name = "S_i_j_0"}
    return %alloc : memref<10x10xi32>
  }
}
void kernel(
  float v0[10][10],
  uint32_t v1[10][10]
) {     // L2
  l_S_i_j_0_i: for (int i = 0; i < 10; i++) {   // L4
    l_j: for (int j = 0; j < 10; j++) { // L5
      float v4 = v0[i][j];      // L6
      uint32_t v5;
      union { float from; uint32_t to;} _converter_v4_to_v5;
      _converter_v4_to_v5.from = v4;
      v5 = _converter_v4_to_v5.to;      // L7
      v1[i][j] = v5;    // L8
    }
  }
}

Checklist

  • PR's title starts with a category (e.g. [Bugfix], [IR], [Builder], etc)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage (It would be better to provide ~2 different test cases to test the robustness of your code)
  • Code is well-documented

@chhzh123 chhzh123 merged commit 71667ce into cornell-zhang:main Jan 4, 2025
1 check passed
@chhzh123 chhzh123 deleted the bitcast branch January 4, 2025 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant