Skip to content
Merged
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
47 changes: 26 additions & 21 deletions llvm_util/llvm2alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1739,36 +1739,34 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
}
}

static FPDenormalAttrs::Type parse_fp_denormal_str(string_view str) {
if (str == "dynamic") return FPDenormalAttrs::Dynamic;
if (str == "ieee") return FPDenormalAttrs::IEEE;
if (str == "preserve-sign") return FPDenormalAttrs::PreserveSign;
if (str == "positive-zero") return FPDenormalAttrs::PositiveZero;
UNREACHABLE();
static FPDenormalAttrs::Type parse_fp_denormal(llvm::DenormalMode::DenormalModeKind mode) {
switch (mode) {
case llvm::DenormalMode::IEEE:
return FPDenormalAttrs::IEEE;
case llvm::DenormalMode::PositiveZero:
return FPDenormalAttrs::PositiveZero;
case llvm::DenormalMode::PreserveSign:
return FPDenormalAttrs::PreserveSign;
case llvm::DenormalMode::Dynamic:
return FPDenormalAttrs::Dynamic;
default:
UNREACHABLE();
}
}

static FPDenormalAttrs parse_fp_denormal(string_view str) {
FPDenormalAttrs attr;
auto comma = str.find(',');
if (comma == string_view::npos) {
attr.input = attr.output = parse_fp_denormal_str(str);
} else {
attr.output = parse_fp_denormal_str(string_view(str.data(), comma));
attr.input = parse_fp_denormal_str(str.data() + comma + 1);
}
return attr;
static FPDenormalAttrs parse_fp_denormal(llvm::DenormalMode mode) {
return {
.input = parse_fp_denormal(mode.Input),
.output = parse_fp_denormal(mode.Output),
};
}

static void handleFnAttrs(const llvm::AttributeSet &aset, FnAttrs &attrs) {
for (const llvm::Attribute &llvmattr : aset) {
if (llvmattr.isStringAttribute()) {
auto str = llvmattr.getKindAsString();
auto val = llvmattr.getValueAsString();
if (str == "denormal-fp-math") {
attrs.setFPDenormal(parse_fp_denormal(val));
} else if (str == "denormal-fp-math-f32") {
attrs.setFPDenormal(parse_fp_denormal(val), 32);
} else if (str == "alloc-family") {
if (str == "alloc-family") {
attrs.allocfamily = val;
}
}
Expand All @@ -1790,6 +1788,13 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
attrs.allocsize_1 = *args.second;
break;
}
case llvm::Attribute::DenormalFPEnv: {
auto fp_env = llvmattr.getDenormalFPEnv();
attrs.setFPDenormal(parse_fp_denormal(fp_env.DefaultMode));
if (fp_env.F32Mode != fp_env.DefaultMode)
attrs.setFPDenormal(parse_fp_denormal(fp_env.F32Mode), 32);
break;
}
case llvm::Attribute::AllocKind: {
auto kind = llvmattr.getAllocKind();
if ((kind & llvm::AllocFnKind::Alloc) != llvm::AllocFnKind::Unknown)
Expand Down
9 changes: 9 additions & 0 deletions tests/alive-tv/fp/denormal-attrs-mismatch.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; ERROR: Function attributes not refined

define float @src(float %x) denormal_fpenv(positivezero) {
ret float %x
}

define float @tgt(float %x) {
ret float %x
}
9 changes: 9 additions & 0 deletions tests/alive-tv/fp/denormal-ieee-default.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; this transformation is OK since ieee is the default

define float @src(float %x) denormal_fpenv(ieee) {
ret float %x
}

define float @tgt(float %x) {
ret float %x
}
4 changes: 2 additions & 2 deletions tests/alive-tv/fp/fadd-denormal.srctgt.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
define double @src() "denormal-fp-math"="positive-zero" {
define double @src() denormal_fpenv(positivezero) {
%result = fadd double 0x8000000000000, 0.0
ret double %result
}

define double @tgt() "denormal-fp-math"="positive-zero" {
define double @tgt() denormal_fpenv(positivezero) {
ret double 0.0
}
8 changes: 8 additions & 0 deletions tests/alive-tv/fp/fdiv-denormal-f32-fold.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define float @src() denormal_fpenv(float: positivezero) {
%result = fdiv float 0x3810000000000000, 2.000000e+00
ret float %result
}

define float @tgt() denormal_fpenv(float: positivezero) {
ret float 0.0
}
12 changes: 12 additions & 0 deletions tests/alive-tv/fp/fdiv-denormal-f32-no-fold-1.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; ERROR: Value mismatch

; the "float: ieee" here overrides the positivezero for the float type

define float @src() denormal_fpenv(positivezero, float: ieee) {
%result = fdiv float 0x3810000000000000, 2.000000e+00
ret float %result
}

define float @tgt() denormal_fpenv(positivezero, float: ieee) {
ret float 0.0
}
12 changes: 12 additions & 0 deletions tests/alive-tv/fp/fdiv-denormal-f32-no-fold-2.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; ERROR: Value mismatch

; the float attribute should not apply to doubles

define double @src() denormal_fpenv(float: positivezero) {
%result = fdiv double 0x3810000000000000, 2.000000e+00
ret double %result
}

define double @tgt() denormal_fpenv(float: positivezero) {
ret double 0.0
}
8 changes: 8 additions & 0 deletions tests/alive-tv/fp/fdiv-denormal-fpenv.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define float @src() denormal_fpenv(positivezero|ieee) {
%result = fdiv float 0x3810000000000000, -2.000000e+00
ret float %result
}

define float @tgt() denormal_fpenv(positivezero|ieee) {
ret float 0.0
}
4 changes: 2 additions & 2 deletions tests/alive-tv/fp/fneg-denormal.srctgt.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
define double @src() "denormal-fp-math"="positive-zero" {
define double @src() denormal_fpenv(positivezero) {
%result = fneg double 0x8000000000000
ret double %result
}

define double @tgt() "denormal-fp-math"="positive-zero" {
define double @tgt() denormal_fpenv(positivezero) {
ret double 0x8008000000000000
}
8 changes: 4 additions & 4 deletions tests/alive-tv/params/fn-refinement.srctgt.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
define double @src() "denormal-fp-math"="positive-zero" {
; ERROR: Function attributes not refined

define double @src() denormal_fpenv(positivezero) {
%result = fadd double 0x8000000000000, 0.0
ret double %result
}

define double @tgt() "denormal-fp-math"="ieee" {
define double @tgt() denormal_fpenv(ieee) {
ret double 0.0
}

; ERROR: Function attributes not refined
Loading