{"id":251,"date":"2016-01-19T07:53:51","date_gmt":"2016-01-18T21:53:51","guid":{"rendered":"http:\/\/anton.ozlabs.org\/blog\/?p=251"},"modified":"2016-01-19T12:34:50","modified_gmt":"2016-01-19T02:34:50","slug":"swift-on-power-linux","status":"publish","type":"post","link":"https:\/\/anton.ozlabs.org\/blog\/2016\/01\/19\/swift-on-power-linux\/","title":{"rendered":"Swift on POWER Linux"},"content":{"rendered":"<p>Apple open sourced the Swift language recently, and I&#8217;ve been meaning to take a look at how much work it would be to port it to POWER Linux.<\/p>\n<p>As with many languages that use LLVM, it turned\u00c2\u00a0out to be relatively straightforward. I submitted a patch a few days ago, and thanks to some great assistance from\u00c2\u00a0Dmitri Gribenko in reviewing my work, support for little endian PowerPC64 is already\u00c2\u00a0<a href=\"https:\/\/github.com\/apple\/swift\/commit\/b1827d8a8fb49c4b2a22e7a8c11ab768cdb904e8\">upstream<\/a>.<\/p>\n<p>A couple of things made the port go smoothly. Firstly the compiler is written in C++ and not Swift. It seems\u00c2\u00a0<a href=\"https:\/\/github.com\/apple\/swift\/blob\/2c7b0b22831159396fe0e98e5944e64a483c356e\/www\/FAQ.rst\">rewrite the Swift compiler in Swift<\/a>\u00c2\u00a0is a common request, but sticking to C++ makes the bootstrap process so much easier. I&#8217;ve had to bootstrap other compilers (Rust) that are written in their target language, and\u00c2\u00a0that requires a cross build from a supported architecture which\u00c2\u00a0never goes smoothly.<\/p>\n<p>More often than not, languages need to link to C objects and libraries, so they need knowledge of\u00c2\u00a0the C calling convention rules. Much\u00c2\u00a0of this knowledge is not in LLVM (it&#8217;s in the Clang frontend), and as a result\u00c2\u00a0most LLVM based languages end up duplicating it (<a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/master\/src\/librustc_trans\/trans\/cabi_powerpc64.rs\">Rust<\/a>, Julia).\u00c2\u00a0Uli Weigand pointed out to me that\u00c2\u00a0Swift pulls in Clang\u00c2\u00a0to provide this, which really helps. With all the duplication across languages, it might make sense to move this logic into an LLVM library instead of Clang.<\/p>\n<p>Even though the port is upstream, there is one required LLVM change\u00c2\u00a0<a href=\"https:\/\/llvm.org\/bugs\/show_bug.cgi?id=26190\">here<\/a>\u00c2\u00a0&#8211; Swift has some calling conventions that are different to C, and I&#8217;m hoping one of the\u00c2\u00a0POWER LLVM team will take pity on me and sort it out.<\/p>\n<p>Building Swift on POWER\u00c2\u00a0is straightforward on Ubuntu 15.10, just remember to add the LLVM patch above after checking\u00c2\u00a0out the source.<\/p>\n<p>The testsuite runs with only a couple of failures:<\/p>\n<pre class=\"console-output\">Failing Tests (2):\r\n    Swift :: 1_stdlib\/VarArgs.swift\r\n    Swift :: IRGen\/c_layout.sil\r\n\r\n  Expected Passes    : 7061\r\n  Expected Failures  : 78\r\n  Unsupported Tests  : 699\r\n  Unexpected Failures: 2\r\n<\/pre>\n<p>And more importantly, hello world works:<\/p>\n<pre># uname -m\r\nppc64le\r\n\r\n# cat hello.swift\r\nprint(\"Hello, world!\")\r\n\r\n# swift hello.swift\r\nHello, world!<\/pre>\n<p>There is still work to be done to get big endian PowerPC64 going, but little endian seems solid for any of your hello world needs.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apple open sourced the Swift language recently, and I&#8217;ve been meaning to take a look at how much work it would be to port it to POWER Linux. As with many languages that use LLVM, it turned\u00c2\u00a0out to be relatively straightforward. I submitted a patch a few days ago, and thanks to some great assistance &hellip; <a href=\"https:\/\/anton.ozlabs.org\/blog\/2016\/01\/19\/swift-on-power-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Swift on POWER Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/251"}],"collection":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":16,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/251\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}