[PATCH] D111500: [InstSimplify] Simplify intrinsic comparisons with domain knoweldge

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 9 15:52:28 PDT 2021


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, tra.
Herald added a subscriber: hiraditya.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch adds support for simplifying instrinstic comparisons using
domain knowledge. In this case, a comparison with the NVPTX instrinstic
returning the number of threads and the number of threads in the block
will always be true. We can fold this accordingly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111500

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/intrinsic.ll


Index: llvm/test/Transforms/InstSimplify/intrinsic.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstSimplify/intrinsic.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+define i32 @compare() {
+; CHECK-LABEL: @compare(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br i1 true, label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    br label [[RETURN:%.*]]
+; CHECK:       if.else:
+; CHECK-NEXT:    br label [[RETURN]]
+; CHECK:       return:
+; CHECK-NEXT:    [[RETVAL:%.*]] = phi i32 [ 1, [[IF_THEN]] ], [ 0, [[IF_ELSE]] ]
+; CHECK-NEXT:    ret i32 [[RETVAL]]
+;
+entry:
+  %tid = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
+  %ntid = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+  %cmp = icmp slt i32 %tid, %ntid
+  br i1 %cmp, label %if.then, label %if.else
+
+if.then:                                          ; preds = %entry
+  br label %return
+
+if.else:                                          ; preds = %entry
+  br label %return
+
+return:                                           ; preds = %if.else, %if.then
+  %retval = phi i32 [ 1, %if.then ], [ 0, %if.else ]
+  ret i32 %retval
+}
+
+declare i32 @llvm.nvvm.read.ptx.sreg.tid.x()
+
+declare i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -39,6 +39,7 @@
 #include "llvm/IR/GlobalAlias.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicsNVPTX.h"
 #include "llvm/IR/Operator.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/ValueHandle.h"
@@ -599,6 +600,20 @@
   return CommonValue;
 }
 
+static Constant *foldIntrinsicConstant(ICmpInst::Predicate Pred, Value *Op0,
+                                       Value *Op1, Type *RetTy) {
+  IntrinsicInst *Inst0 = dyn_cast<IntrinsicInst>(Op0);
+  IntrinsicInst *Inst1 = dyn_cast<IntrinsicInst>(Op1);
+
+  // fold %cmp = icmp slt i32 %tid, %ntid to true.
+  if (Inst0->getIntrinsicID() == Intrinsic::nvvm_read_ptx_sreg_tid_x &&
+      Inst1->getIntrinsicID() == Intrinsic::nvvm_read_ptx_sreg_ntid_x)
+    if (ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred))
+      return ConstantInt::getTrue(RetTy);
+
+  return nullptr;
+}
+
 static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
                                        Value *&Op0, Value *&Op1,
                                        const SimplifyQuery &Q) {
@@ -3703,6 +3718,12 @@
     if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
       return V;
 
+  // If the comparison is with two instrinsic instructions try to fold them
+  // using domain knowledge.
+  if (isa<IntrinsicInst>(LHS) && isa<IntrinsicInst>(RHS))
+    if (Constant *C = foldIntrinsicConstant(Pred, LHS, RHS, ITy))
+      return C;
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111500.378481.patch
Type: text/x-patch
Size: 3071 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211009/707af86d/attachment.bin>


More information about the llvm-commits mailing list