Hi!

It’s been a while since I proposed this change! Here is a set of
patches that does this in a more clean way than the last patch (which
fiddled with the random seed in the compiler, ugh…)

The four patches are independent and address different points of the
code that make the build process unstable. I hope everything is clear,
feel free to ask me if anything isn’t.

These changes will make the life far more easy for NixOS/Guix package
maintainers, and makes it far more easy to see how changes in the
compiler affect its output.

As a bonus, CHICKEN can be used in conjunction with ccache for a
much greater compilation speed! \o/

----

Building CHICKEN twice using ccache, with and without the deterministic build 
patch.
Clean cache for each test.

Without patch
=============

326.60u 8.78s 335.58r    make PLATFORM=linux C_COMPILER=ccache gcc

cache directory                     /tmp/ccache
primary config                      /tmp/ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
cache hit (direct)                     2
cache hit (preprocessed)               4
cache miss                           224
cache hit rate                      2.61 %
called for link                      110
cleanups performed                     0
files in cache                       450
cache size                          41.6 MB
max cache size                       5.0 GB


With patch
==========

63.73u 2.34s 66.10r      make PLATFORM=linux C_COMPILER=ccache gcc

cache directory                     /tmp/ccache
primary config                      /tmp/ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
cache hit (direct)                   114
cache hit (preprocessed)               1
cache miss                           115
cache hit rate                     50.00 %
called for link                      110
cleanups performed                     0
files in cache                       229
cache size                          21.1 MB
max cache size                       5.0 GB
>From 4eb5fcc3466cf8a3d34e84171c91fe5035ea83f0 Mon Sep 17 00:00:00 2001
From: Kooda <[email protected]>
Date: Sat, 21 Oct 2017 11:58:07 +0200
Subject: [PATCH 1/4] Sort the symbol table before outputing C code from the
 complier.

This helps make the compiler deterministic, as the output will not change
because of the random seeding of the symbol table.
---
 c-backend.scm | 67 +++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 23 deletions(-)

diff --git a/c-backend.scm b/c-backend.scm
index c8b48335..4262cc51 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -72,11 +72,24 @@
 (define (uncommentify s) (string-translate* (->string s) '(("*/" . "*_/"))))
 (define (c-identifier s) (string->c-identifier (->string s)))
 
+;; Generate a sorted alist out of a symbol table
+(define (table->sorted-alist t)
+  (let ((alist '()))
+    (hash-table-for-each
+      (lambda (id ll)
+	(set! alist
+	  (cons (cons id ll) alist)))
+      t)
+
+    (sort! alist (lambda (p1 p2) (string<? (symbol->string (car p1))
+					   (symbol->string (car p2)))))))
+
 
 ;;; Generate target code:
 
 (define (generate-code literals lliterals lambda-table out source-file user-supplied-options dynamic db dbg-info-table)
-  (let ((non-av-proc #f))
+  (let ((lambda-table* (table->sorted-alist lambda-table)) ;; sort the symbol table to make the compiler output deterministic.
+	(non-av-proc #f))
 
   ;; Don't truncate floating-point precision!
   (flonum-print-precision (+ flonum-maximum-decimal-exponent 1))
@@ -614,9 +627,11 @@
   
     (define (prototypes)
       (gen #t)
-      (hash-table-for-each
-       (lambda (id ll)
-	 (let* ((n (lambda-literal-argument-count ll))
+      (for-each
+       (lambda (p)
+	 (let* ((id (car p))
+		(ll (cdr p))
+		(n (lambda-literal-argument-count ll))
 		(customizable (lambda-literal-customizable ll))
 		(empty-closure (and customizable (zero? (lambda-literal-closure-size ll))))
 		(varlist (intersperse (make-variable-list (if empty-closure (sub1 n) n) "t") #\,))
@@ -649,7 +664,7 @@
 	   (gen #\))
 	   (unless direct (gen " C_noret"))
 	   (gen #\;) ))
-       lambda-table) )
+       lambda-table*) )
   
     (define (trampolines)
       (let ([ns '()]
@@ -662,9 +677,11 @@
 	      ((>= i n))
 	    (gen #t "C_word t" i "=av[" j "];")))
 
-	(hash-table-for-each
-	 (lambda (id ll)
-	   (let* ([argc (lambda-literal-argument-count ll)]
+	(for-each
+	 (lambda (p)
+	   (let* ([id (car p)]
+		  [ll (cdr p)]
+		  [argc (lambda-literal-argument-count ll)]
 		  [rest (lambda-literal-rest-argument ll)]
 		  [rest-mode (lambda-literal-rest-argument-mode ll)]
 		  [customizable (lambda-literal-customizable ll)]
@@ -679,7 +696,7 @@
 	       (let ([al (make-argument-list argc "t")])
 		 (apply gen (intersperse al #\,)) )
 	       (gen ");}") )))
-	 lambda-table)))
+	 lambda-table*)))
   
     (define (literal-frame)
       (do ([i 0 (add1 i)]
@@ -775,9 +792,11 @@
 	(else (bomb "invalid unboxed type" t))))
 
     (define (procedures)
-      (hash-table-for-each
-       (lambda (id ll)
-	 (let* ((n (lambda-literal-argument-count ll))
+      (for-each
+       (lambda (p)
+	 (let* ((id (car p))
+		(ll (cdr p))
+		(n (lambda-literal-argument-count ll))
 		(rname (real-name id db))
 		(demand (lambda-literal-allocated ll))
 		(max-av (apply max 0 (lambda-literal-callee-signatures ll)))
@@ -925,7 +944,7 @@
 		n)
 	    ll)
 	   (gen #\}) ) )
-       lambda-table) )
+       lambda-table*) )
 
     (debugging 'p "code generation phase...")
     (set! output out)
@@ -939,7 +958,7 @@
     (when emit-debug-info
       (emit-debug-table dbg-info-table))
     (procedures)
-    (emit-procedure-table lambda-table source-file)
+    (emit-procedure-table lambda-table* source-file)
     (trailer) ) )
 
 
@@ -961,16 +980,18 @@
 
 ;;; Emit procedure table:
 
-(define (emit-procedure-table lambda-table sf)
+(define (emit-procedure-table lambda-table* sf)
   (gen #t #t "#ifdef C_ENABLE_PTABLES"
-       #t "static C_PTABLE_ENTRY ptable[" (add1 (hash-table-size lambda-table)) "] = {")
-  (hash-table-for-each
-   (lambda (id ll)
-     (gen #t "{\"" id #\: (string->c-identifier sf) "\",(void*)")
-     (if (eq? 'toplevel id)
-         (gen "C_" (toplevel unit-name) "},")
-         (gen id "},") ) )
-   lambda-table)
+       #t "static C_PTABLE_ENTRY ptable[" (add1 (length lambda-table*)) "] = {")
+  (for-each
+   (lambda (p)
+     (let ((id (car p))
+	   (ll (cdr p)))
+       (gen #t "{\"" id #\: (string->c-identifier sf) "\",(void*)")
+       (if (eq? 'toplevel id)
+	   (gen "C_" (toplevel unit-name) "},")
+	   (gen id "},") ) ) )
+    lambda-table*)
   (gen #t "{NULL,NULL}};")
   (gen #t "#endif")
   (gen #t #t "static C_PTABLE_ENTRY *create_ptable(void)")
-- 
2.15.0.rc1

>From 5ea38e499e84d811972aea4a75badc25832d3fe2 Mon Sep 17 00:00:00 2001
From: Kooda <[email protected]>
Date: Sat, 21 Oct 2017 13:25:33 +0200
Subject: [PATCH 2/4] Remove the date from the header comment of generated C
 files.

---
 c-backend.scm | 57 +++++++++++++++++++++++----------------------------------
 1 file changed, 23 insertions(+), 34 deletions(-)

diff --git a/c-backend.scm b/c-backend.scm
index 4262cc51..d7b8844b 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -554,40 +554,29 @@
       (expr node temps) )
  
     (define (header)
-      (define (pad0 n)
-	(if (< n 10)
-	    (string-append "0" (number->string n))
-	    n) )
-      (let* ((tm (##sys#decode-seconds (current-seconds) #f))
-	     (min (vector-ref tm 1))
-	     (hour (vector-ref tm 2))
-	     (mday (vector-ref tm 3))
-	     (mon (vector-ref tm 4))
-	     (year (vector-ref tm 5)) )
-	(gen "/* Generated from " source-file " by the CHICKEN compiler" #t
-	     "   http://www.call-cc.org"; #t
-	     "   " (+ 1900 year) #\- (pad0 (add1 mon)) #\- (pad0 mday) #\space (pad0 hour) #\: (pad0 min) #t
-	     (string-intersperse
-	      (map (cut string-append "   " <> "\n") 
-		   (string-split (chicken-version #t) "\n") ) 
-	      "")
-	     "   command line: ")
-	(gen-list user-supplied-options)
-	(unless (not unit-name)
-	  (gen #t "   unit: " unit-name))
-	(unless (null? used-units)
-	  (gen #t "   uses: ")
-	  (gen-list used-units))
-        (gen #t "*/")
-	(gen #t "#include \"" target-include-file "\"")
-	(when external-protos-first
-	  (generate-foreign-callback-stub-prototypes foreign-callback-stubs) )
-	(when (pair? foreign-declarations)
-	  (gen #t)
-	  (for-each (lambda (decl) (gen #t decl)) foreign-declarations) )
-	(unless external-protos-first
-	  (generate-foreign-callback-stub-prototypes foreign-callback-stubs) ) ) )
-  
+      (gen "/* Generated from " source-file " by the CHICKEN compiler" #t
+	   "   http://www.call-cc.org"; #t
+	   (string-intersperse
+	    (map (cut string-append "   " <> "\n")
+		 (string-split (chicken-version #t) "\n") )
+	    "")
+	   "   command line: ")
+      (gen-list user-supplied-options)
+      (unless (not unit-name)
+	(gen #t "   unit: " unit-name))
+      (unless (null? used-units)
+	(gen #t "   uses: ")
+	(gen-list used-units))
+      (gen #t "*/")
+      (gen #t "#include \"" target-include-file "\"")
+      (when external-protos-first
+	(generate-foreign-callback-stub-prototypes foreign-callback-stubs) )
+      (when (pair? foreign-declarations)
+	(gen #t)
+	(for-each (lambda (decl) (gen #t decl)) foreign-declarations) )
+      (unless external-protos-first
+	(generate-foreign-callback-stub-prototypes foreign-callback-stubs) ) )
+
     (define (trailer)
       (gen #t #t "/*" #t 
 	   (uncommentify
-- 
2.15.0.rc1

>From 829911e3c956f4303bc6f9bd06e40b2905079acb Mon Sep 17 00:00:00 2001
From: Kooda <[email protected]>
Date: Sat, 21 Oct 2017 13:26:21 +0200
Subject: [PATCH 3/4] Make the building of .a files deterministic

---
 defaults.make | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/defaults.make b/defaults.make
index 741bb6a9..e43d8d73 100644
--- a/defaults.make
+++ b/defaults.make
@@ -154,7 +154,7 @@ LINKER_LIBRARY_SUFFIX ?=
 endif
 LINKER_LINK_SHARED_LIBRARY_OPTIONS ?= -shared
 LINKER_LINK_SHARED_DLOADABLE_OPTIONS ?= -shared -L.
-LIBRARIAN_OPTIONS ?= cru
+LIBRARIAN_OPTIONS ?= crD
 LIBRARIAN_OUTPUT_OPTION ?=
 LIBRARIAN_OUTPUT ?= $(LIBRARIAN_OUTPUT_OPTION) $@
 LIBRARIES ?= -lm
-- 
2.15.0.rc1

>From dea8ffc2f5630a2471a1940c503f9d1cba637ce3 Mon Sep 17 00:00:00 2001
From: Kooda <[email protected]>
Date: Sat, 21 Oct 2017 13:50:45 +0200
Subject: [PATCH 4/4] Remove the build tag completely.

---
 build-version.scm     | 3 ---
 defaults.make         | 1 -
 distribution/manifest | 1 -
 identify.sh           | 6 ------
 library.scm           | 4 +---
 rules.make            | 7 ++-----
 6 files changed, 3 insertions(+), 19 deletions(-)

diff --git a/build-version.scm b/build-version.scm
index ff2565b4..c113a7d8 100644
--- a/build-version.scm
+++ b/build-version.scm
@@ -27,8 +27,6 @@
 (declare
  (unit build-version))
 
-(foreign-declare "#include \"buildtag.h\"")
-
 ;; (read-version filename): Read line from FILENAME and return
 ;; as a string; return #f if non-existent file or blank line.
 (define-syntax read-version
@@ -43,7 +41,6 @@
 		     #f
 		     ver)))))))))
 
-(define (##sys#build-tag)   (foreign-value "C_BUILD_TAG" c-string))
 (define ##sys#build-id      (read-version "buildid"))
 (define ##sys#build-branch  (read-version "buildbranch"))
 (define ##sys#build-version (read-version "buildversion"))
diff --git a/defaults.make b/defaults.make
index e43d8d73..9ba2a6f7 100644
--- a/defaults.make
+++ b/defaults.make
@@ -201,7 +201,6 @@ SCRIPT_EXT = .bat
 COPY_COMMAND = copy /Y
 HOSTNAME ?= $(shell hostname)
 UNAME_SYS ?= Windows
-BUILD_TAG ?= compiled $(BUILD_TIME) on $(HOSTNAME) ($(UNAME_SYS))
 # This is a poor man's version of $(file ...) in GNU Make 4.0
 # We should consider replacing it when it becomes so widespread
 # that systems (Debian, OS X, Haiku, Mingw, Cygwin) are shipping it
diff --git a/distribution/manifest b/distribution/manifest
index 29bf2aff..802a9a07 100644
--- a/distribution/manifest
+++ b/distribution/manifest
@@ -113,7 +113,6 @@ user-pass.scm
 build-version.scm
 build-version.c
 buildid
-buildtag.h
 tests/clustering-tests.scm
 tests/c-id-valid.scm
 tests/data-structures-tests.scm
diff --git a/identify.sh b/identify.sh
index 0a398d12..23912a29 100755
--- a/identify.sh
+++ b/identify.sh
@@ -12,7 +12,6 @@ fi
 
 rev0=`cat buildid || echo ""`
 branchname0=`cat buildbranch || echo ""`
-tag0=`cat buildtag.h || echo ""`
 buildtime=`date +%Y-%m-%d`
 host=`hostname`
 usys=`uname`
@@ -20,14 +19,12 @@ usys=`uname`
 if test -d "$1/.git"; then
     rev=`GIT_DIR="$1/.git" git rev-parse --short HEAD 2>/dev/null`
     branchname=`GIT_DIR="$1/.git" git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
-    tag="#define C_BUILD_TAG \"compiled ${buildtime} on ${host} (${usys})\""
 elif test -d "$1/manual-html"; then
     # Assume this is a snapshot or release build; don't touch
     # identifying files to avoid a rebuild which needs chicken
     exit
 else
     branchname=${branchname:-"custom"}
-    tag=${tag:-"#define C_BUILD_TAG \"compiled ${buildtime} on ${host} (${usys})\""}
 fi
 
 case "${branchname}" in
@@ -42,6 +39,3 @@ fi
 if test "x${branchname0}" \!= "x${branchname}"; then
     echo ${branchname} >buildbranch
 fi
-if test "x${tag0}" \!= "x${tag}"; then
-    echo ${tag} >buildtag.h
-fi
diff --git a/library.scm b/library.scm
index d7c9387e..a95bd195 100644
--- a/library.scm
+++ b/library.scm
@@ -6093,9 +6093,7 @@ EOF
 	 (get-config)
 	 (if (zero? (##sys#size spec))
 	     ""
-	     (string-append " [" spec " ]"))
-	 "\n"
-	 (or (##sys#build-tag) "")))
+	     (string-append " [" spec " ]"))))
       ##sys#build-version))
 
 ;;; Installation locations
diff --git a/rules.make b/rules.make
index 1bfccfc3..b3799667 100644
--- a/rules.make
+++ b/rules.make
@@ -457,8 +457,6 @@ endif
 # build versioning
 
 ifdef WINDOWS_SHELL
-buildtag.h:
-	echo #define C_BUILD_TAG "$(BUILD_TAG)" >$@
 buildbranch:
 	echo.$(BRANCHNAME)>buildbranch
 buildid:
@@ -469,7 +467,6 @@ else
 identify-me:
 	@sh $(SRCDIR)identify.sh $(SRCDIR)
 
-buildtag.h: identify-me
 buildbranch: identify-me
 buildid: identify-me
 endif
@@ -883,7 +880,7 @@ stub.c: $(SRCDIR)stub.scm $(SRCDIR)common-declarations.scm
 	$(bootstrap-lib) 
 debugger-client.c: $(SRCDIR)debugger-client.scm $(SRCDIR)common-declarations.scm dbg-stub.c
 	$(bootstrap-lib)
-build-version.c: $(SRCDIR)build-version.scm $(SRCDIR)buildversion buildbranch buildid buildtag.h
+build-version.c: $(SRCDIR)build-version.scm $(SRCDIR)buildversion buildbranch buildid
 	$(bootstrap-lib)
 
 define declare-bootstrap-import-lib
@@ -965,7 +962,7 @@ confclean:
 
 spotless: distclean testclean
 	-$(REMOVE_COMMAND) $(REMOVE_COMMAND_OPTIONS) $(DISTFILES) \
-	  buildtag.h buildid buildbranch
+	buildid buildbranch
 
 
 distclean: clean confclean
-- 
2.15.0.rc1

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to