Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isSigned usage for scalar prints. #201

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

ienkovich
Copy link
Collaborator

Resolves #200

@ienkovich ienkovich requested review from int3, minjang and Devjiu December 23, 2024 17:39
@ienkovich ienkovich requested a review from ptillet as a code owner December 23, 2024 17:39
Copy link

@stephen-huan stephen-huan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to send a PR, was figuring out some build things. Can confirm this PR fixes the example in #200.

dtype int32
(0, 0, 0) n: -1

Seems almost identical!

diff --git a/third_party/cpu/lib/TritonCPUToLLVM/DebugOpsToLLVM.cpp b/third_party/cpu/lib/TritonCPUToLLVM/DebugOpsToLLVM.cpp
index 21b4756b..5599ef8d 100644
--- a/third_party/cpu/lib/TritonCPUToLLVM/DebugOpsToLLVM.cpp
+++ b/third_party/cpu/lib/TritonCPUToLLVM/DebugOpsToLLVM.cpp
@@ -32,8 +32,10 @@ public:
 
 // TODO: This code is the same as the GPU-backend code. Consider refactoring.
 std::string getFormatSubstr(Value value, bool hex = false,
-                            std::optional<int> width = std::nullopt) {
+                            std::optional<int> width = std::nullopt,
+                            bool isSigned = false) {
   Type type = value.getType();
+  // If the `value` is a pointer, just return %p.
   if (isa<LLVM::LLVMPointerType>(type)) {
     return "%p";
   }
@@ -54,21 +56,16 @@ std::string getFormatSubstr(Value value, bool hex = false,
     prefix += std::to_string(*width);
   } else if (hex) {
     prefix += "0";
-    prefix += std::to_string(value.getType().getIntOrFloatBitWidth() / 4);
+    prefix += std::to_string(type.getIntOrFloatBitWidth() / 4);
   }
 
   if (type.isBF16() || type.isF16() || type.isF32() || type.isF64()) {
     return prefix + "f";
-  } else if (type.isSignedInteger()) {
+  } else if (type.isInteger()) {
     if (type.getIntOrFloatBitWidth() == 64)
-      return prefix + "lli";
+      return prefix + (isSigned ? "lli" : "llu");
     else
-      return prefix + "i";
-  } else if (type.isUnsignedInteger() || type.isSignlessInteger()) {
-    if (type.getIntOrFloatBitWidth() == 64)
-      return prefix + "llu";
-    else
-      return prefix + "u";
+      return prefix + (isSigned ? "i" : "u");
   }
   assert(false && "not supported type");
   return "";
@@ -163,7 +160,8 @@ static StringRef makeNullTerminatedString(StringRef s) {
 
 void createRuntimePrintScalarCall(ConversionPatternRewriter &rewriter,
                                   std::array<Value, 3> pid, StringRef prefix,
-                                  std::optional<Value> arg, bool hex = false) {
+                                  std::optional<Value> arg,
+                                  bool isSigned = false, bool hex = false) {
   assert(!prefix.empty() && "printf with empty string not supported");
   auto loc = UnknownLoc::get(rewriter.getContext());
 
@@ -172,7 +170,7 @@ void createRuntimePrintScalarCall(ConversionPatternRewriter &rewriter,
   os << "(" << getFormatSubstr(pid[0]) << ", " << getFormatSubstr(pid[1])
      << ", " << getFormatSubstr(pid[2]) << ")" << prefix;
   if (arg.has_value())
-    os << getFormatSubstr(arg.value(), hex);
+    os << getFormatSubstr(arg.value(), hex, std::nullopt, isSigned);
 
   llvm::SmallString<64> formatStrNewline(formatStr);
   formatStrNewline.push_back('\n');
@@ -242,7 +240,8 @@ struct PrintOpConversion : public ConvertOpToLLVMPattern<triton::cpu::PrintOp> {
                                      std::nullopt);
       } else {
         createRuntimePrintScalarCall(rewriter, pid, op.getPrefix(),
-                                     adaptor.getOperands()[0], op.getHex());
+                                     adaptor.getOperands()[0],
+                                     op.getIsSigned()[0], op.getHex());
       }
       rewriter.eraseOp(op);
       return success();

Copy link
Collaborator

@minjang minjang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@minjang
Copy link
Collaborator

minjang commented Dec 23, 2024

Merging it as a quick patch!

@minjang minjang merged commit daa7eb0 into triton-lang:main Dec 23, 2024
3 checks passed
@ienkovich ienkovich deleted the ienkovich/print-neg-scalar branch December 23, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

device_print treats ints as unsigned
3 participants