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

[AutoBump] Merge with fixes of d4f97da1 (Aug 27) (13) #366

Open
wants to merge 3 commits into
base: bump_to_4b7f07a0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mlir/docs/Dialects/emitc.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ The following convention is followed:
or any of the C++ headers in which the type is defined.
* If `emitc.array` with a dimension of size zero, then the code
requires [a GCC extension](https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html).
* If `_Float16` is used, the code requires the support of C additional
floating types.
* If `__bf16` is used, the code requires a compiler that supports it, such as
GCC or Clang.
* Else the generated code is compatible with C99.

These restrictions are neither inherent to the EmitC dialect itself nor to the
Expand Down
16 changes: 15 additions & 1 deletion mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,21 @@ bool mlir::emitc::isIntegerIndexOrOpaqueType(Type type) {
}

bool mlir::emitc::isSupportedFloatType(Type type) {
return isa<Float32Type, Float64Type>(type);
if (auto floatType = llvm::dyn_cast<FloatType>(type)) {
switch (floatType.getWidth()) {
case 16: {
if (llvm::isa<Float16Type, BFloat16Type>(type))
return true;
return false;
}
case 32:
case 64:
return isa<Float32Type, Float64Type>(type);
default:
return false;
}
}
return false;
}

bool mlir::emitc::isPointerWideType(Type type) {
Expand Down
28 changes: 22 additions & 6 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,12 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
val.toString(strValue, 0, 0, false);
os << strValue;
switch (llvm::APFloatBase::SemanticsToEnum(val.getSemantics())) {
case llvm::APFloatBase::S_IEEEhalf:
os << "f16";
break;
case llvm::APFloatBase::S_BFloat:
os << "bf16";
break;
case llvm::APFloatBase::S_IEEEsingle:
os << "f";
break;
Expand All @@ -1277,17 +1283,19 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {

// Print floating point attributes.
if (auto fAttr = dyn_cast<FloatAttr>(attr)) {
if (!isa<Float32Type, Float64Type>(fAttr.getType())) {
return emitError(loc,
"expected floating point attribute to be f32 or f64");
if (!isa<Float16Type, BFloat16Type, Float32Type, Float64Type>(
fAttr.getType())) {
return emitError(
loc, "expected floating point attribute to be f16, bf16, f32 or f64");
}
printFloat(fAttr.getValue());
return success();
}
if (auto dense = dyn_cast<DenseFPElementsAttr>(attr)) {
if (!isa<Float32Type, Float64Type>(dense.getElementType())) {
return emitError(loc,
"expected floating point attribute to be f32 or f64");
if (!isa<Float16Type, BFloat16Type, Float32Type, Float64Type>(
dense.getElementType())) {
return emitError(
loc, "expected floating point attribute to be f16, bf16, f32 or f64");
}
os << '{';
interleaveComma(dense, os, [&](const APFloat &val) { printFloat(val); });
Expand Down Expand Up @@ -1640,6 +1648,14 @@ LogicalResult CppEmitter::emitType(Location loc, Type type) {
}
if (auto fType = dyn_cast<FloatType>(type)) {
switch (fType.getWidth()) {
case 16: {
if (llvm::isa<Float16Type>(type))
return (os << "_Float16"), success();
else if (llvm::isa<BFloat16Type>(type))
return (os << "__bf16"), success();
else
return emitError(loc, "cannot emit float type ") << type;
}
case 32:
return (os << "float"), success();
case 64:
Expand Down
54 changes: 10 additions & 44 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,35 @@ func.func @arith_cast_vector(%arg0: vector<5xf32>) -> vector<5xi32> {
}

// -----

func.func @arith_cast_bf16(%arg0: bf16) -> i32 {
func.func @arith_cast_f80(%arg0: f80) -> i32 {
// expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
%t = arith.fptosi %arg0 : bf16 to i32
%t = arith.fptosi %arg0 : f80 to i32
return %t: i32
}

// -----

func.func @arith_cast_f16(%arg0: f16) -> i32 {
func.func @arith_cast_f128(%arg0: f128) -> i32 {
// expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
%t = arith.fptosi %arg0 : f16 to i32
%t = arith.fptosi %arg0 : f128 to i32
return %t: i32
}


// -----

func.func @arith_cast_to_bf16(%arg0: i32) -> bf16 {
func.func @arith_cast_to_f80(%arg0: i32) -> f80 {
// expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
%t = arith.sitofp %arg0 : i32 to bf16
return %t: bf16
%t = arith.sitofp %arg0 : i32 to f80
return %t: f80
}

// -----

func.func @arith_cast_to_f16(%arg0: i32) -> f16 {
func.func @arith_cast_to_f128(%arg0: i32) -> f128 {
// expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
%t = arith.sitofp %arg0 : i32 to f16
return %t: f16
%t = arith.sitofp %arg0 : i32 to f128
return %t: f128
}

// -----
Expand Down Expand Up @@ -135,23 +134,6 @@ func.func @arith_remui_vector(%arg0: vector<5xi32>, %arg1: vector<5xi32>) -> vec
return %divui: vector<5xi32>
}

// -----

func.func @arith_extf_to_bf16(%arg0: f8E4M3FN) {
// expected-error @+1 {{failed to legalize operation 'arith.extf'}}
%ext = arith.extf %arg0 : f8E4M3FN to bf16
return
}

// -----

func.func @arith_extf_to_f16(%arg0: f8E4M3FN) {
// expected-error @+1 {{failed to legalize operation 'arith.extf'}}
%ext = arith.extf %arg0 : f8E4M3FN to f16
return
}


// -----

func.func @arith_extf_to_tf32(%arg0: f8E4M3FN) {
Expand Down Expand Up @@ -194,22 +176,6 @@ func.func @arith_truncf_to_tf32(%arg0: f64) {

// -----

func.func @arith_truncf_to_f16(%arg0: f64) {
// expected-error @+1 {{failed to legalize operation 'arith.truncf'}}
%trunc = arith.truncf %arg0 : f64 to f16
return
}

// -----

func.func @arith_truncf_to_bf16(%arg0: f64) {
// expected-error @+1 {{failed to legalize operation 'arith.truncf'}}
%trunc = arith.truncf %arg0 : f64 to bf16
return
}

// -----

func.func @arith_truncf_to_f8E4M3FN(%arg0: f64) {
// expected-error @+1 {{failed to legalize operation 'arith.truncf'}}
%trunc = arith.truncf %arg0 : f64 to f8E4M3FN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ memref.global "nested" constant @nested_global : memref<3x7xf32>

// -----

func.func @unsupported_type_f16() {
func.func @unsupported_type_f128() {
// expected-error@+1 {{failed to legalize operation 'memref.alloca'}}
%0 = memref.alloca() : memref<4xf16>
%0 = memref.alloca() : memref<4xf128>
return
}

Expand Down
16 changes: 0 additions & 16 deletions mlir/test/Dialect/EmitC/invalid_types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@ func.func @illegal_f8E5M2FNUZ_type(%arg0: f8E5M2FNUZ, %arg1: f8E5M2FNUZ) {

// -----

func.func @illegal_f16_type(%arg0: f16, %arg1: f16) {
// expected-error @+1 {{'emitc.mul' op operand #0 must be floating-point type supported by EmitC or integer, index or opaque type supported by EmitC, but got 'f16'}}
%mul = "emitc.mul" (%arg0, %arg1) : (f16, f16) -> f16
return
}

// -----

func.func @illegal_bf16_type(%arg0: bf16, %arg1: bf16) {
// expected-error @+1 {{'emitc.mul' op operand #0 must be floating-point type supported by EmitC or integer, index or opaque type supported by EmitC, but got 'bf16'}}
%mul = "emitc.mul" (%arg0, %arg1) : (bf16, bf16) -> bf16
return
}

// -----

func.func @illegal_f80_type(%arg0: f80, %arg1: f80) {
// expected-error @+1 {{'emitc.mul' op operand #0 must be floating-point type supported by EmitC or integer, index or opaque type supported by EmitC, but got 'f80'}}
%mul = "emitc.mul" (%arg0, %arg1) : (f80, f80) -> f80
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Target/Cpp/const.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ func.func @emitc_constant() {
%c6 = "emitc.constant"(){value = 2 : index} : () -> index
%c7 = "emitc.constant"(){value = 2.0 : f32} : () -> f32
%f64 = "emitc.constant"(){value = 4.0 : f64} : () -> f64
%f16 = "emitc.constant"(){value = 2.0 : f16} : () -> f16
%bf16 = "emitc.constant"(){value = 4.0 : bf16} : () -> bf16
%c8 = "emitc.constant"(){value = dense<0> : tensor<i32>} : () -> tensor<i32>
%c9 = "emitc.constant"(){value = dense<[0, 1]> : tensor<2xindex>} : () -> tensor<2xindex>
%c10 = "emitc.constant"(){value = dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>} : () -> tensor<2x2xf32>
Expand All @@ -26,6 +28,8 @@ func.func @emitc_constant() {
// CPP-DEFAULT-NEXT: size_t [[V6:[^ ]*]] = 2;
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = 2.000000000e+00f;
// CPP-DEFAULT-NEXT: double [[F64:[^ ]*]] = 4.00000000000000000e+00;
// CPP-DEFAULT-NEXT: _Float16 [[F16:[^ ]*]] = 2.00000e+00f16;
// CPP-DEFAULT-NEXT: __bf16 [[BF16:[^ ]*]] = 4.0000e+00bf16;
// CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
// CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
Expand All @@ -40,6 +44,8 @@ func.func @emitc_constant() {
// CPP-DECLTOP-NEXT: size_t [[V6:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[V7:[^ ]*]];
// CPP-DECLTOP-NEXT: double [[F64:[^ ]*]];
// CPP-DECLTOP-NEXT: _Float16 [[F16:[^ ]*]];
// CPP-DECLTOP-NEXT: __bf16 [[BF16:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<int32_t> [[V8:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]];
Expand All @@ -52,6 +58,8 @@ func.func @emitc_constant() {
// CPP-DECLTOP-NEXT: [[V6]] = 2;
// CPP-DECLTOP-NEXT: [[V7]] = 2.000000000e+00f;
// CPP-DECLTOP-NEXT: [[F64]] = 4.00000000000000000e+00;
// CPP-DECLTOP-NEXT: [[F16]] = 2.00000e+00f16;
// CPP-DECLTOP-NEXT: [[BF16]] = 4.0000e+00bf16;
// CPP-DECLTOP-NEXT: [[V8]] = {0};
// CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
// CPP-DECLTOP-NEXT: [[V10]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
4 changes: 4 additions & 0 deletions mlir/test/Target/Cpp/types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func.func @ptr_types() {
emitc.call_opaque "f"() {template_args = [!emitc.ptr<i32>]} : () -> ()
// CHECK-NEXT: f<int64_t*>();
emitc.call_opaque "f"() {template_args = [!emitc.ptr<i64>]} : () -> ()
// CHECK-NEXT: f<_Float16*>();
emitc.call_opaque "f"() {template_args = [!emitc.ptr<f16>]} : () -> ()
// CHECK-NEXT: f<__bf16*>();
emitc.call_opaque "f"() {template_args = [!emitc.ptr<bf16>]} : () -> ()
// CHECK-NEXT: f<float*>();
emitc.call_opaque "f"() {template_args = [!emitc.ptr<f32>]} : () -> ()
// CHECK-NEXT: f<double*>();
Expand Down