Skip to content

[CODEGEN] Enhance OpenCL Generator to Support Conditional Generation that Preserves Type #2787

@lixiaoquan

Description

@lixiaoquan

This code (simplified) can't be built with both NV OpenCL and llvm. Error is "error: call to 'max' is ambiguous"

   uchar i = 0, j = 0;
   uchar t = max((uchar)j, ((i > 0) ? (uchar)1 : (uchar)0));

It seems the type of the ternary operator is not unchar.

My current workaround is to always add a conversion for uint8 before tvm::if_then_else, if you think it's OK, I'll send a PR.

diff --git a/tvm/src/codegen/codegen_c.cc b/tvm/src/codegen/codegen_c.cc
index 9b73e4e..3a5357a 100644
--- a/tvm/src/codegen/codegen_c.cc
+++ b/tvm/src/codegen/codegen_c.cc
@@ -524,6 +524,13 @@ void CodeGenC::VisitExpr_(const Call *op, std::ostream& os) {  // NOLINT(*)
   } else if (op->is_intrinsic(Call::shift_right)) {
     PrintBinaryIntrinsitc(op, " >> ", os, this);
   } else if (op->is_intrinsic(intrinsic::tvm_if_then_else)) {
+    // Workaround OpenCL compiler issue
+    // type of ternary expression in NV OpenCL compiler isn't same as type of true/false value
+    if (op->args[2].type() == UInt(8)) {
+      os << "(";
+      PrintType(UInt(8), os);
+      os << ")";
+    }
     os << "(";
     PrintExpr(op->args[0], os);
     os << " ? ";

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions