This is an archive of the discontinued LLVM Phabricator instance.

[mlir][AMDGPU] Improve BF16 handling through AMDGPU compilation
ClosedPublic

Authored by krzysz00 on Jul 26 2023, 1:16 PM.

Details

Summary

Many previous sets of AMDGPU dialect code have been incorrect in the
presence of the bf16 type (when lowered to LLVM's bfloat) as they were
developed in a setting that run a custom bf16-to-i16 pass before LLVM
lowering.

An overall effect of this patch is that you should run
--arith-emulate-unsupported-floats="source-types=bf16 target-type=f32"
on your GPU module before calling --convert-gpu-to-rocdl if your code
performs bf16 arithmetic.

While LLVM now supports software bfloat, initial experiments showed
that using this support on AMDGPU inserted a large number of
conversions around loads and stores which had substantial performance
imparts. Furthermore, all of the native AMDGPU operations on bf16
types (like the WMMA operations) operate on 16-bit integers instead of
the bfloat type.

First, we make the following changes to preserve compatibility once
the LLVM bfloat type is reenabled.

  1. The matrix multiplication operations (MFMA and WMMA) will bitcast

bfloat vectors to i16 vectors.

  1. Buffer loads and stores will operate on the relevant integer

datatype and then cast to bfloat if needed.

Second, we add type conversions to convert bf16 and vectors of it to
equivalent i16 types.

Third, we add the bfloat <-> f32 expansion patterns to the set of
operations run before the main LLVM conversion so that MLIR's
implementation of these conversion routines is used.

Finally, we extend the "floats treated as integers" support in the
LLVM exporter to handle types other than fp8.

We also fix a bug in the unsupported floats emulation where it tried
to operate on arith.bitcast due to an oversight.

Diff Detail

Event Timeline

krzysz00 created this revision.Jul 26 2023, 1:16 PM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a reviewer: kuhar. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
krzysz00 requested review of this revision.Jul 26 2023, 1:16 PM
fmorac added a subscriber: fmorac.
rsuderman accepted this revision.Aug 16 2023, 2:48 PM
This revision is now accepted and ready to land.Aug 16 2023, 2:48 PM