Skip to content

Commit

Permalink
[DAGCombiner] Fold fold (fp_to_bf16 (bf16_to_fp op)) -> op
Browse files Browse the repository at this point in the history
  • Loading branch information
d0k committed Jun 15, 2022
1 parent aaff3fb commit 8c4a07c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Expand Up @@ -510,6 +510,7 @@ namespace {
SDValue visitMSCATTER(SDNode *N);
SDValue visitFP_TO_FP16(SDNode *N);
SDValue visitFP16_TO_FP(SDNode *N);
SDValue visitFP_TO_BF16(SDNode *N);
SDValue visitVECREDUCE(SDNode *N);
SDValue visitVPOp(SDNode *N);

Expand Down Expand Up @@ -1746,6 +1747,7 @@ SDValue DAGCombiner::visit(SDNode *N) {
case ISD::LIFETIME_END: return visitLIFETIME_END(N);
case ISD::FP_TO_FP16: return visitFP_TO_FP16(N);
case ISD::FP16_TO_FP: return visitFP16_TO_FP(N);
case ISD::FP_TO_BF16: return visitFP_TO_BF16(N);
case ISD::FREEZE: return visitFREEZE(N);
case ISD::VECREDUCE_FADD:
case ISD::VECREDUCE_FMUL:
Expand Down Expand Up @@ -23072,6 +23074,16 @@ SDValue DAGCombiner::visitFP16_TO_FP(SDNode *N) {
return SDValue();
}

SDValue DAGCombiner::visitFP_TO_BF16(SDNode *N) {
SDValue N0 = N->getOperand(0);

// fold (fp_to_bf16 (bf16_to_fp op)) -> op
if (N0->getOpcode() == ISD::BF16_TO_FP)
return N0->getOperand(0);

return SDValue();
}

SDValue DAGCombiner::visitVECREDUCE(SDNode *N) {
SDValue N0 = N->getOperand(0);
EVT VT = N0.getValueType();
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/X86/bfloat.ll
Expand Up @@ -100,3 +100,16 @@ define void @store_constant(ptr %pc) {
store bfloat 1.0, ptr %pc
ret void
}

define void @fold_ext_trunc(ptr %pa, ptr %pc) {
; CHECK-LABEL: fold_ext_trunc:
; CHECK: # %bb.0:
; CHECK-NEXT: movzwl (%rdi), %eax
; CHECK-NEXT: movw %ax, (%rsi)
; CHECK-NEXT: retq
%a = load bfloat, ptr %pa
%ext = fpext bfloat %a to float
%trunc = fptrunc float %ext to bfloat
store bfloat %trunc, ptr %pc
ret void
}

0 comments on commit 8c4a07c

Please sign in to comment.