LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 11933 - clang crashes on long double constants on powerpc
Summary: clang crashes on long double constants on powerpc
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Support Libraries (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-06 06:33 PST by Richard Pennington
Modified: 2012-11-01 16:08 PDT (History)
10 users (show)

See Also:
Fixed By Commit(s):


Attachments
Test case (26 bytes, text/x-c)
2012-02-06 06:33 PST, Richard Pennington
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Pennington 2012-02-06 06:33:26 PST
Created attachment 8004 [details]
Test case

The attached test case fails when compiled for the ppc and ppc64.
APFloat.cpp does not completely understand the PowerPC long double format.
This failure was introduced between r149184 and r149781.

ecc: /home/rich/ellcc/llvm/lib/Support/APFloat.cpp:121: void assertArithmeticOK(const llvm::fltSemantics&): Assertion `semantics.arithmeticOK && "Compile-time arithmetic does not support these semantics"' failed.
0  ecc             0x0000000001f3c47f
1  ecc             0x0000000001f3cfba
2  libpthread.so.0 0x0000003ecca0eeb0
3  libc.so.6       0x0000003ecbe330c5 gsignal + 53
4  libc.so.6       0x0000003ecbe34a76 abort + 390
5  libc.so.6       0x0000003ecbe2b905 __assert_fail + 245
6  ecc             0x0000000001ef6e8e
7  ecc             0x0000000000a9ddef clang::Sema::ActOnNumericConstant(clang::Token const&) + 1103
8  ecc             0x0000000000950bf8 clang::Parser::ParseCastExpression(bool, bool, bool&, clang::Parser::TypeCastState) + 1272
9  ecc             0x000000000095355a clang::Parser::ParseCastExpression(bool, bool, clang::Parser::TypeCastState) + 42
10 ecc             0x0000000000953fff clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) + 31
11 ecc             0x00000000009302f0 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&) + 560
12 ecc             0x000000000093539c clang::Parser::ParseDeclGroup(clang::Parser::ParsingDeclSpec&, unsigned int, bool, clang::SourceLocation*, clang::Parser::ForRangeInit*) + 700
13 ecc             0x000000000091f04b clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsingDeclSpec&, clang::AccessSpecifier) + 155
14 ecc             0x000000000091f76e clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::AccessSpecifier) + 766
15 ecc             0x0000000000921e68 clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::Parser::ParsingDeclSpec*) + 2872
16 ecc             0x0000000000922474 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 164
17 ecc             0x00000000008f1cbd clang::ParseAST(clang::Sema&, bool) + 221
18 ecc             0x0000000000785ad4 clang::CodeGenAction::ExecuteAction() + 68
19 ecc             0x00000000006346c4 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 324
20 ecc             0x0000000000617ba5 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1701
21 ecc             0x000000000060ba79 cc1_main(char const**, char const**, char const*, void*) + 7065
22 ecc             0x000000000061634f main + 9311
23 libc.so.6       0x0000003ecbe1ee5d __libc_start_main + 253
24 ecc             0x0000000000609079
Stack dump:
0.      Program arguments: /home/rich/ellcc/bin/ecc -cc1 -triple ppc-ellcc-linux -S -disable-free -main-file-name ldhex.c -mrelocation-model static -mdisable-fp-elim -mconstructor-aliases -msoft-float -target-linker-version 2.20.51.0.7 -momit-leaf-frame-pointer -resource-dir /home/rich/ellcc/bin/../libecc -fmodule-cache-path /var/tmp/clang-module-cache -fno-dwarf-directory-asm -fdebug-compilation-dir /home/rich/ellcc/libecc/obj/ppc -ferror-limit 19 -fmessage-length 110 -mstackrealign -fno-signed-char -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-fragile-abi -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/ldhex-ovVdff.s -x c ldhex.c 
1.      ldhex.c:1:17: current parser token '0x1p514L'
ecc: error: unable to execute command: Aborted (core dumped)
ecc: error: clang frontend command failed due to signal (use -v to see invocation)
ecc: note: diagnostic msg: Please submit a bug report to http://llvm.org/bugs/ and include command line arguments and all diagnostic information.
Comment 1 Justin Hibbits 2012-02-14 10:53:08 PST
I see the same issue with decimal long double constants.
Comment 2 Hal Finkel 2012-02-24 20:04:55 PST
I can reproduce this with any long-double constant. This fails:
const long double v = 0.0L;
int main()
{
	return 0;
}
clang: /src/llvm-trunk-dev-q/lib/Support/APFloat.cpp:121: void assertArithmeticOK(const llvm::fltSemantics&): Assertion `semantics.arithmeticOK && "Compile-time arithmetic does not support these semantics"' failed.
...
1.	ld.cpp:1:23: current parser token '0.0L'
Comment 3 Hal Finkel 2012-02-24 20:36:45 PST
A backtrace of the problem shows:
(gdb) up
#3  0x00000000028758d1 in assertArithmeticOK (semantics=...) at /src/llvm-trunk/lib/Support/APFloat.cpp:120
120	  assert(semantics.arithmeticOK &&
(gdb) up
#4  0x0000000002875c79 in llvm::APFloat::APFloat (this=0x7fffffff9390, ourSemantics=...)
    at /src/llvm-trunk/lib/Support/APFloat.cpp:741
741	  assertArithmeticOK(ourSemantics);
(gdb) list
736	  significandParts()[0] = value;
737	  normalize(rmNearestTiesToEven, lfExactlyZero);
738	}
739	
740	APFloat::APFloat(const fltSemantics &ourSemantics) : exponent2(0), sign2(0) {
741	  assertArithmeticOK(ourSemantics);
742	  initialize(&ourSemantics);
743	  category = fcZero;
744	  sign = false;
745	}
(gdb) up
#5  0x0000000000e0c18f in clang::Sema::ActOnNumericConstant (this=0x37da7a0, Tok=...)
    at /src/llvm-trunk/tools/clang/lib/Sema/SemaExpr.cpp:2428
2428	    APFloat Val(Format);
(gdb) list
2423	      Ty = Context.LongDoubleTy;
2424	
2425	    const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
2426	
2427	    using llvm::APFloat;
2428	    APFloat Val(Format);
2429	
2430	    APFloat::opStatus result = Literal.GetFloatValue(Val);
2431	
2432	    // Overflow is always an error, but underflow is only an error if

So the "problem" is that the APFloat constructor calls assertArithmeticOK which will fail for PPCDoubleDouble values. Why is it doing this?
Comment 4 Hal Finkel 2012-02-24 20:55:32 PST
The problem was introduced in r149121, and this problem was known at the time:
see: http://llvm.org/bugs/show_bug.cgi?id=11867
Comment 5 Jeremy Huddleston Sequoia 2012-02-25 13:10:11 PST
Yep, but it isn't a clang bug.  It's an llvm bug.  The PPC encoding just isn't implemented in llvm yet.
Comment 6 David Fang 2012-04-04 01:53:00 PDT
I now see this issue on powerpc-darwin8, bug 12444.  (This prevents me from compiling hello-world.)
I'm willing to help test if any patches are posted.
Comment 7 David Fang 2012-04-23 16:53:42 PDT
Is there any chance of this being done for 3.1?
Comment 8 Hal Finkel 2012-04-24 07:32:00 PDT
(In reply to comment #7)
> Is there any chance of this being done for 3.1?

Unfortunately, I don't think so. I've started working on a fix, but it is not ready yet. For now, I'd recommend locally applying:

diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp
index 409d4fb..5141125 100644
--- a/lib/Support/APFloat.cpp
+++ b/lib/Support/APFloat.cpp
@@ -61,7 +61,8 @@ namespace llvm {
   // The PowerPC format consists of two doubles.  It does not map cleanly
   // onto the usual format above.  For now only storage of constants of
   // this type is supported, no arithmetic.
-  const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
+  // FIXME: arithmeticOK should really be false here...
+  const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, true };
 
   /* A tight upper bound on number of parts required to hold the value
      pow(5, power) is

This will allow parsing to not crash, but it will not really be correct, and long double codegen is still also not quite right.
Comment 9 David Fang 2012-04-25 18:48:23 PDT
(In reply to comment #8)
> (In reply to comment #7)
> Unfortunately, I don't think so. I've started working on a fix, but it is not
> ready yet. For now, I'd recommend locally applying:
> 
> This will allow parsing to not crash, but it will not really be correct, and
> long double codegen is still also not quite right.

Thanks, Hal.  Just having it not crash is valuable.  I do understand that it's not complete, but I don't plan to exercise much long double code myself.
Comment 10 David Fang 2012-10-31 15:28:00 PDT
What's the status of this after Ulrich Weigand's recent commits (ca. r166951)?
Comment 11 Hal Finkel 2012-10-31 15:34:59 PDT
(In reply to comment #10)
> What's the status of this after Ulrich Weigand's recent commits (ca. r166951)?

Thanks for pointing this out... this is now resolved.
Comment 12 Jeremy Huddleston Sequoia 2012-10-31 15:39:13 PDT
MacPorts' llvm-3.2 port is at r166880.  I'm testing an update to r167157 right now and will probably commit it in the next day or so to allow MacPorts users to easily test this.