From e13ca363a7e54604122a05e7d51ae4e50a5998d1 Mon Sep 17 00:00:00 2001 From: Daniel Gorin Date: Sun, 24 Apr 2022 16:53:26 +0100 Subject: [PATCH 1/2] Fix bitrot in {default,shell}.nix It was no longer possible to run the tests using nix-shell as instructed in the README. There were several issues: - default.nix had outdated dependencies - default.nix had `doCheck` set to false, which meant that the test dependencies (tasty, etc) wouldn't show up in the derivation and would not be installed. - haskell deps are to be found in `(propagated}buildInputs` - the pinned version of nixpkgs was very old, and prettyprinter was not available there - stdenv.lib was deprecated long ago and it is no longer available in recent versions of nixpkgs Because of the last point, running nix-shell would effectively only install ghc and cabal-install, and cabal would to build llvm-hs, which is needed only for running the tests. With the above fixed, running nix-shell would try to build llvm-hs and llvm-hs-pure and fail: the default.nix files there have also bitrot, have missing dependencies, etc. Because llvm-hs and llvm-hs-pure are available in nixpkgs now, we can rely on those versions instead. I'm leaving the support for running versions from source, which can be useful at some point once they are fixed upstream. --- default.nix | 14 +++++++------- shell.nix | 16 +++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/default.nix b/default.nix index 133e133..aae68c0 100644 --- a/default.nix +++ b/default.nix @@ -1,21 +1,21 @@ -{ mkDerivation, base, bytestring, directory, filepath, llvm-hs -, llvm-hs-pure, mtl, pretty-show, stdenv, tasty, tasty-golden -, tasty-hspec, tasty-hunit, text, transformers, wl-pprint-text +{ mkDerivation, array, base, bytestring, directory, filepath, lib +, llvm-hs llvm-hs-pure, mtl, prettyprinter, tasty, tasty-golden +, tasty-hspec, tasty-hunit, text, transformers }: mkDerivation { pname = "llvm-hs-pretty"; version = "0.1.0.0"; src = ./.; libraryHaskellDepends = [ - base bytestring llvm-hs-pure text wl-pprint-text + array base bytestring llvm-hs-pure text prettyprinter ]; testHaskellDepends = [ - base directory filepath llvm-hs llvm-hs-pure mtl pretty-show tasty + base directory filepath llvm-hs llvm-hs-pure mtl tasty tasty-golden tasty-hspec tasty-hunit text transformers ]; doHaddock = false; - doCheck = false; + doCheck = true; homepage = "https://github.com/llvm-hs/llvm-hs-pretty"; description = "Pretty printer for LLVM IR"; - license = stdenv.lib.licenses.mit; + license = lib.licenses.mit; } diff --git a/shell.nix b/shell.nix index 7472c29..e442ac8 100644 --- a/shell.nix +++ b/shell.nix @@ -2,12 +2,12 @@ let default_nixpkgs = (import {}).fetchFromGitHub { owner = "NixOS"; repo = "nixpkgs"; - rev = "68cc97d306d3187c142cfb2378852f28d47bc098"; - sha256 = "07zxbk4g4d51hf7dhsj6h7jy5c2iccm2lwaashj36inkhh9lrqa3"; + rev = "a7ecde854aee5c4c7cd6177f54a99d2c1ff28a31"; # 21.11 + sha256 = "162dywda2dvfj1248afxc45kcrg83appjd0nmdb541hl7rnncf02"; }; in -{ nixpkgs ? default_nixpkgs }: +{ nixpkgs ? default_nixpkgs, llvm_hs_deps_from_source ? false }: let @@ -16,8 +16,8 @@ let llvm-hs-repo = orig_pkgs.fetchFromGitHub { owner = "llvm-hs"; repo = "llvm-hs"; - rev = "76cd4d5107862401a7ebbe1bb9cc1cf172fa1d66"; - sha256 = "0bnh0yyjflhvc8vjrqsa25k7issnvkvgx149bnq7avka5mx2m99m"; + rev = "442bc488c39f0264930c95e2c98b5cf055d53e8e"; + sha256 = "1xdxy9gcgs8y32hvvmx2bl9i3h6z967v77g4yp3blqwc2kmbrpg8"; }; hsOverlay = self: super: { @@ -25,14 +25,14 @@ let overrides = self': super': { llvm-hs-pure = super'.callPackage (import "${llvm-hs-repo}/llvm-hs-pure") {}; llvm-hs = super'.callPackage (import "${llvm-hs-repo}/llvm-hs") { - llvm-config = self.llvm_4; + llvm-config = self.llvm_9; }; llvm-hs-pretty = super'.callPackage ./. {}; }; }; }; - pkgs = import orig_pkgs.path { overlays = [ hsOverlay ]; }; + pkgs = import orig_pkgs.path { overlays = if llvm_hs_deps_from_source then [ hsOverlay ] else []; }; env = let @@ -44,6 +44,8 @@ let allDependencies = let inherit (pkgs.haskellPackages) llvm-hs-pretty; in builtins.concatLists [ + llvm-hs-pretty.buildInputs + llvm-hs-pretty.propagatedBuildInputs llvm-hs-pretty.nativeBuildInputs llvm-hs-pretty.propagatedNativeBuildInputs ] From 9362e24a6540d069dd6d1524d7c87ea00d4f4b7a Mon Sep 17 00:00:00 2001 From: Daniel Gorin Date: Sun, 24 Apr 2022 17:26:44 +0100 Subject: [PATCH 2/2] Use named reference for tuples when available The added test-case would previously fail since: ``` ; ModuleID = 'simple module' %struct.coord2d = type {i32, i32} %struct.vector = type {%struct.coord2d, %struct.coord2d} @up = global %struct.vector {%struct.coord2d {i32 0, i32 0}, %struct.coord2d {i32 0, i32 1}} ``` would get pretty-printed as invalid code: ``` ; ModuleID = 'simple module' %struct.coord2d = type {i32, i32} %struct.vector = type {%struct.coord2d, %struct.coord2d} @up = global %struct.vector {%struct.coord2d zeroinitializer, {i32 0, i32 0} {i32 0, i32 1}} ``` --- src/LLVM/Pretty/Typed.hs | 4 +++- tests/input/named-struct.ll | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/input/named-struct.ll diff --git a/src/LLVM/Pretty/Typed.hs b/src/LLVM/Pretty/Typed.hs index 2b6e96a..0bf8799 100644 --- a/src/LLVM/Pretty/Typed.hs +++ b/src/LLVM/Pretty/Typed.hs @@ -38,7 +38,9 @@ instance Typed C.Constant where typeOf (C.Float t) = typeOf t typeOf (C.Null t) = t typeOf (C.AggregateZero t) = t - typeOf (C.Struct {..}) = StructureType isPacked (map typeOf memberValues) + typeOf (C.Struct {..}) = case structName of + Just name -> NamedTypeReference name + Nothing -> StructureType isPacked (map typeOf memberValues) typeOf (C.Array {..}) = ArrayType (fromIntegral $ length memberValues) memberType typeOf (C.Vector {..}) = VectorType (fromIntegral $ length memberValues) $ case memberValues of diff --git a/tests/input/named-struct.ll b/tests/input/named-struct.ll new file mode 100644 index 0000000..c0ae427 --- /dev/null +++ b/tests/input/named-struct.ll @@ -0,0 +1,6 @@ +; ModuleID = 'simple module' + +%struct.coord2d = type {i32, i32} +%struct.vector = type {%struct.coord2d, %struct.coord2d} + +@up = global %struct.vector {%struct.coord2d {i32 0, i32 0}, %struct.coord2d {i32 0, i32 1}}