llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

AsmWriter produces invalid IR when using certain locales (comma ',' instead of periods '.')

Open llvmbot opened this issue 14 years ago • 11 comments

Bugzilla Link 12906
Resolution FIXED
Resolved on Nov 16, 2017 17:34
Version trunk
OS Linux
Attachments [Compile and run with LANG=sv_SE.UTF-8 .https://user-images.githubusercontent.com/60944935/143745370-f14cb4a8-1140-4b85-836c-948aaf9e8305.gz)
Reporter LLVM Bugzilla Contributor
CC @hfinkel,@pitrou,@rnk,@serguei-katkov

Extended Description

Hi. I have my LANG environment variable set to LANG=sv_SE.UTF-8 since I live in Sweden. In my program I also use setlocale(LC_ALL, "") before using LLVM libraries.

If I know write printf("%e\n", 1.8); I get 1,800000e+00. If I omit the setlocale(LC_ALL, ""); I get 1.800000e+00 instead.

LLVM uses snprintf to print out doubles in AsmWriter, which (of course) produces strings with comma instead of period, when locale settings are set as they are above. That is incorrect, since LLVM only allows doubles with periods and not commas. I even get the assertion:

/home/ubuntu/llvm/lib/Support/APFloat.cpp:285: void interpretDecimal(llvm::StringRef::iterator, llvm::StringRef::iterator, decimalInfo*): Assertion `(*p == 'e' || *p == 'E') && "Invalid character in significand"' failed. Aborted (core dumped)

Stack backtrace: #​4 0xb64a9147 in __GI___assert_fail ( assertion=0xb7c01690 "(p == 'e' || p == 'E') && "Invalid character in significand"", file=0xb7c01264 "/home/ubuntu/llvm/lib/Support/APFloat.cpp", line=285, function=0xb7c029a0 "void interpretDecimal(llvm::StringRef::iterator, llvm::StringRef::iterator, decimalInfo)") at assert.c:103 #​5 0xb75b108f in llvm::APFloat::convertFromDecimalString(llvm::StringRef, llvm::APFloat::roundingMode) () from /usr/lib/libLLVM-3.2svn.so #​6 0xb75b1227 in llvm::APFloat::convertFromString(llvm::StringRef, llvm::APFloat::roundingMode) () from /usr/lib/libLLVM-3.2svn.so #​7 0xb75b1371 in llvm::APFloat::APFloat(llvm::fltSemantics const&, llvm::StringRef) () from /usr/lib/libLLVM-3.2svn.so #​8 0xb6ed1df0 in WriteConstantInternal(llvm::raw_ostream&, llvm::Constant const, (anonymous namespace)::TypePrinting&, (anonymous namespace)::SlotTracker*, llvm::Module const*) () from /usr/lib/libLLVM-3.2svn.so #​9 0xb6ecd00f in (anonymous namespace)::AssemblyWriter::writeOperand(llvm::Value const*, bool) () from /usr/lib/libLLVM-3.2svn.so #​10 0xb6ecdcde in (anonymous namespace)::AssemblyWriter::printInstruction(llvm::Instruction const&) () from /usr/lib/libLLVM-3.2svn.so #​11 0xb6ecf729 in (anonymous namespace)::AssemblyWriter::printBasicBlock(llvm::BasicBlock const*) () from /usr/lib/libLLVM-3.2svn.so #​12 0xb6ecfee1 in (anonymous namespace)::AssemblyWriter::printFunction(llvm::Function const*) () from /usr/lib/libLLVM-3.2svn.so #​13 0xb6ed0c11 in llvm::Module::print(llvm::raw_ostream&, llvm::AssemblyAnnotationWriter*) const () ---Type to continue, or q to quit--- from /usr/lib/libLLVM-3.2svn.so #​14 0x0804a25f in llvm::operator<<(llvm::raw_ostream&, llvm::Module const&) () #​15 0x08049a5d in main ()

This comes from the example HowToUseJIT, by adding setlocale(LC_ALL, ""); as the first line in main(), and having LANG environment variable set as sv_SE.UTF-8 with Ubuntu 12.04.

It is WriteConstantInternal in AsmWriter.cpp that uses SmallString<128> StrVal; raw_svector_ostream(StrVal) << Val; to write a double.

llvmbot avatar May 21 '12 22:05 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#25784

pitrou avatar Nov 26 '21 21:11 pitrou

yes.

serguei-katkov avatar Nov 17 '17 01:11 serguei-katkov

This was fixed in r300943, right?

rnk avatar Nov 16 '17 18:11 rnk

When internationalization goes wrong.

Sorry I did not catch your sentence...

serguei-katkov avatar Nov 16 '17 04:11 serguei-katkov

When internationalization goes wrong.

llvmbot avatar Nov 15 '17 23:11 llvmbot

I expect that the https://reviews.llvm.org/D32276 should fix this issue.

serguei-katkov avatar Apr 20 '17 08:04 serguei-katkov

I have actually submitted a similar patch in http://reviews.llvm.org/D15375 (sorry for not mentioning it here). It's a bit simpler than yours, it just calls the (semantics, StringRef) constructor.

pitrou avatar Dec 09 '15 21:12 pitrou

Oops, apologies. The Numba issue actually comes from a bug in the IR parser. The IR writer seems fine.

I'm testing the following patch. Let me know if it fixes your issue.

diff --git a/lib/AsmParser/LLLexer.cpp b/lib/AsmParser/LLLexer.cpp index f95a763..d2d3272 100644 --- a/lib/AsmParser/LLLexer.cpp +++ b/lib/AsmParser/LLLexer.cpp @@ -937,7 +937,12 @@ lltok::Kind LLLexer::LexDigitOrNegative() { } }

  • APFloatVal = APFloat(std::atof(TokStart));
  • // Decimal FP constants always start as doubles and get converted later.
  • APFloatVal = APFloat(APFloat::IEEEdouble);
  • StringRef Text(TokStart, CurPtr - TokStart);
  • if (APFloatVal.convertFromDecimalString(Text, APFloat::rmNearestTiesToEven) ==
  •  APFloat::opInvalidOp)
    
  • return lltok::Error; return lltok::APFloat; }

@@ -973,6 +978,11 @@ lltok::Kind LLLexer::LexPositive() { } }

  • APFloatVal = APFloat(std::atof(TokStart));
  • // Decimal FP constants always start as doubles and get converted later.
  • APFloatVal = APFloat(APFloat::IEEEdouble);
  • StringRef Text(TokStart, CurPtr - TokStart);
  • if (APFloatVal.convertFromDecimalString(Text, APFloat::rmNearestTiesToEven) ==
  •  APFloat::opInvalidOp)
    
  • return lltok::Error; return lltok::APFloat; }

rnk avatar Dec 09 '15 21:12 rnk

Oops, apologies. The Numba issue actually comes from a bug in the IR parser. The IR writer seems fine.

pitrou avatar Dec 09 '15 14:12 pitrou

This has been hitting us on Numba. Downstream bug is https://github.com/numba/numba/issues/1569

pitrou avatar Dec 09 '15 13:12 pitrou

*** Bug llvm/llvm-bugzilla-archive#25784 has been marked as a duplicate of this bug. ***

pitrou avatar Dec 09 '15 13:12 pitrou