From commits-return-16556-archive-asf-public=cust-asf.ponee.io@tvm.apache.org Sun Jun 28 23:14:34 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 78961180607 for ; Mon, 29 Jun 2020 01:14:34 +0200 (CEST) Received: (qmail 30485 invoked by uid 500); 28 Jun 2020 23:14:33 -0000 Mailing-List: contact commits-help@tvm.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tvm.apache.org Delivered-To: mailing list commits@tvm.apache.org Received: (qmail 30476 invoked by uid 99); 28 Jun 2020 23:14:33 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Jun 2020 23:14:33 +0000 From: =?utf-8?q?GitBox?= To: commits@tvm.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bincubator-tvm=5D_tqchen_commented_on_a_change_in_p?= =?utf-8?q?ull_request_=235666=3A_=5BTIR=5D=5BBugfix=5D_Improved_massive_bui?= =?utf-8?q?ld_times_caused_by_tir=2Efloormod_and_tir=2Efloordiv=2E_Fixed_Top?= =?utf-8?q?i_testcase=2E?= Message-ID: <159338607377.8807.5639017447609992139.asfpy@gitbox.apache.org> Date: Sun, 28 Jun 2020 23:14:33 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit In-Reply-To: References: tqchen commented on a change in pull request #5666: URL: https://github.com/apache/incubator-tvm/pull/5666#discussion_r446710438 ########## File path: src/tir/transforms/lower_intrin.cc ########## @@ -152,14 +172,35 @@ class IntrinInjecter : public tvm::arith::IRMutatorWithAnalyzer { } } } else { - // uncommon case - DLOG(INFO) << "LowerFloorMod: Cannot decide the sign of divsor and divident"; - PrimExpr rmod = truncmod(op->a, op->b); - // b > 0 && rmod >= 0 -> rmod - // b > 0 && rmod < 0 -> rmod + b - // b < 0 && rmod < 0 -> rmod - // b < 0 && rmod > 0 -> rmod + b - return tir::Select((op->b >= 0 && rmod >= 0) || (op->b < 0 && rmod <= 0), rmod, rmod + op->b); + if (dtype.is_float()) { + // a - floor(a / b) * b + return op->a - (VisitExpr_(tvm::floor(op->a / op->b).as()) * op->b); + } else if (dtype.is_int() && dtype.bits() <= 32) { + /* NOTE: + This must be restricted to int32 or less since floats can losslessly represent integers + only if the number of bits in the mantissa exceeds the number of bits in the integer. + Therefore a double (53 bit mantissa) for int32, float (24 bit mantissa) for int16, etc. + Since there is no float128 type, int64 is not supported. + */ + + // a - floor(a / b) * b + auto fdtype = DataType::Float(dtype.bits() * 2, dtype.lanes()); + auto div = tir::Div(tir::Cast(fdtype, op->a), tir::Cast(fdtype, op->b)); Review comment: let us just use the rule in the else branch ########## File path: src/tir/transforms/lower_intrin.cc ########## @@ -106,14 +106,34 @@ class IntrinInjecter : public tvm::arith::IRMutatorWithAnalyzer { } } } else { - // uncommon case - DLOG(INFO) << "LowerFloorDiv: Cannot decide the sign of divisor"; - // b >= 0 => (rmod >=0 ? rdiv : rdiv - 1) - // b < 0 => (rmod <= 0 ? rdiv : rdiv - 1) - PrimExpr rdiv = truncdiv(op->a, op->b); - PrimExpr rmod = truncmod(op->a, op->b); - return tir::Select((op->b >= 0 && rmod >= 0) || (op->b < 0 && rmod <= 0), rdiv, - rdiv - make_const(dtype, 1)); + if (dtype.is_float()) { + // floor(a / b) + return VisitExpr_(tvm::floor(op->a / op->b).as()); + } else if (dtype.is_int() && dtype.bits() <= 32) { + /* NOTE: + This must be restricted to int32 or less since floats can losslessly represent integers + only if the number of bits in the mantissa exceeds the number of bits in the integer. + Therefore a double (53 bit mantissa) for int32, float (24 bit mantissa) for int16, etc. + Since TVM is unaware of a float128 type, int64 is not supported. + */ + + // floor(a / b) + auto fdtype = DataType::Float(dtype.bits() * 2, dtype.lanes()); Review comment: Let us also use the rules in the else branch instead. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org